Reputation: 707
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.
Upvotes: 3
Views: 3281
Reputation: 707
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();
Upvotes: 4
Reputation: 455
Looks like you're trying to use the BasicAuth protocol to login to a site? If you're getting an HTML page back with a login screen, the site is not using BasicAuth. BasicAuth is those old-style URLs you rarely see like:
username:password@http://somewebsite.com/events.csv
With BasicAuth, your browser handles the authentication with a little popup window.
To access this site with curl, you'll need to go through accessing the login page, filling out the form, submitting, or validating with a cookie, token, or some other method.
Upvotes: 1