0

I am developing a C# wpf application that has a functionality of logging into my website and download the file. This said website has an Authorize attribute on its action. I need 2 cookies for me to able to download the file, first cookie is for me to log in, second cookie(which is provided after successful log in) is for me to download the file. So i came up with the flow of keeping my cookies after my httpwebrequest/httpwebresponse. I am looking at my posting flow as maybe it is the problem. Here is my code.

void externalloginanddownload() { string pageSource = string.Empty; CookieContainer cookies = new CookieContainer(); HttpWebRequest getrequest = (HttpWebRequest)WebRequest.Create("login uri"); getrequest.CookieContainer = cookies; getrequest.Method = "GET"; getrequest.AllowAutoRedirect = false; HttpWebResponse getresponse = (HttpWebResponse)getrequest.GetResponse(); using (StreamReader sr = new StreamReader(getresponse.GetResponseStream())) { pageSource = sr.ReadToEnd(); } var values = new NameValueCollection { {"Username", "username"}, {"Password", "password"}, { "Remember me?","False"}, }; var parameters = new StringBuilder(); foreach (string key in values.Keys) { parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(values[key])); } parameters.Length -= 1; HttpWebRequest postrequest = (HttpWebRequest)WebRequest.Create("login uri"); postrequest.CookieContainer = cookies; postrequest.Method = "POST"; using (var writer = new StreamWriter(postrequest.GetRequestStream())) { writer.Write(parameters.ToString()); } using (WebResponse response = postrequest.GetResponse()) // the error 500 occurs here { using (var streamReader = new StreamReader(response.GetResponseStream())) { string html = streamReader.ReadToEnd(); } } } 
9
  • you haven't mentioned what problem you are having. Where does it fail? Commented Jul 19, 2017 at 5:42
  • it fails gettting the response of the httpwebrequest with a POST method. it gives me an ERROR 500. Commented Jul 19, 2017 at 5:42
  • and why do you do this?: parameters.Length -= 1; Commented Jul 19, 2017 at 5:43
  • i just saw it on one of the SO post. and to figure it out, it is just removing the '&' sign of the last parameter Commented Jul 19, 2017 at 5:45
  • can someone give me another way to download a file from a website which has an [Authorize] attribute? WebClient doesnt seems to work for me. Thanks Commented Jul 19, 2017 at 5:47

2 Answers 2

1

When you get the WebResponse, the cookies returned will be in the response, not in the request (oddly enough, even though you need to CookieContainer on the request).

You will need to add the cookies from the response object to your CookieContainer, so it gets sent on the next request.

One simple way:

for(var cookie in getresponse.Cookies) cookies.Add(cookie) 

Since the cookies in response is already a cookies container, you can do this (might help to check for null in case all cookies were already there)

if (response.Cookies != null) cookies.Add(response.Cookies) 

You may also have trouble with your POST as you need to set ContentType and length:

myWebRequest.ContentLength = parameters.Length; myWebRequest.AllowWriteStreamBuffering = true; 

If you have any multibyte characters to think about, you may have to address that as well by setting the encoding to UTF-8 on the request and the stringbuilder, and converting string to bytes and using that length.

Another tip: some web server code chokes if there is no user agent. Try:

myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; 

And just in case you have any multibyte characters, it is better to do this:

var databytes = System.Text.Encoding.UTF8.GetBytes(parameters.ToString()); myWebRequest.ContentLength = databytes.Length; myWebRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; using (var stream = myWebRequest.GetRequestStream()) { stream.Write(databytes, 0, databytes.Length); } 
Sign up to request clarification or add additional context in comments.

9 Comments

The response should only include new cookies that weren't in the request, so doing this after each request should accomplish what you want
im not sure about the "application/x-www-form-urlencoded" does it mean the values that i am passing to the action is placed on the URL?
"application/x-www-form-urlencoded" basically means that the post parameters are formatted as "key=value&key=value..." as opposed to json encoding or some other type.
I am still getting this error. The remote server returned an error: (500) Internal Server Error.
|
0

In C# Application (Server side Web API) Enable the C++ Exception and Common Language Run time Exceptions using (Ctrl+Alt+E) what is the Server side Exception it's throw.

First you check data is binding Properly. After you can see what it is Exact Exception. the Internal Server Error Mostly throw the data is not correct format and not properly managed Exception.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.