I am trying to make a cURL call to download a CSV file and I'm getting back the login page html. Here is the code:
WebRequest myRequest = WebRequest.Create("https://website.com/medias/823533/events.csv");
myRequest.Credentials = new NetworkCredential("username", "password");
myRequest.Method = "GET";
myRequest.PreAuthenticate = true;
// Return the response.
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
Console.WriteLine(result);
Console.ReadLine();
Is there a simple cURL method in C#? This doesn't seem to be working no matter what I try.
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 incase 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();