Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL call in C#

Tags:

c#

c#-4.0

I need to perform a cURL request to grab a CSV file from the web. I've never used cURL before and so far the stuff I have seen via google doesn't seem to make sense:

the cURL call needs to look like this:

curl --user username:password http://somewebsite.com/events.csv

the code I am trying to use ( i use this to grab basic auth XML files)

string URL = "http://somewebsite.com/events.csv";
string Username = "username";
string Password = "password";
WebClient req = new WebClient();

CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(URL), "Basic", new NetworkCredential(Username, Password));

req.Credentials = myCache;

string results = null;
results = Encoding.UTF8.GetString(req.DownloadData(URL));

This just gives me back the html for a login screen so the authentication is not happening.

like image 273
Sam Cromer Avatar asked Dec 05 '25 08:12

Sam Cromer


1 Answers

I found the issue. Apparently the request fails when you pass an @ sign ( like in the email username ) in the username field so it must be Converted to a base 64 string. Here is the code just in case someone else runs into this:

    string url = "https://website.com/events.csv"; 
    WebRequest myReq = WebRequest.Create(url);      
    string username = "username";
    string password = "password";
    string usernamePassword = username + ":" + password;
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
    myReq.Credentials = mycache;
    myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    WebResponse wr = myReq.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
    Console.WriteLine(content);
    Console.ReadLine();
like image 150
Sam Cromer Avatar answered Dec 07 '25 21:12

Sam Cromer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!