By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

How to maintain HttpWebRequest session from scripted cookie...

P: 1
I have an C# Windows application that is to connect to a remote HTTPS website, POST the login, then subsequently POST a file and get the response. There is plenty of code out there that shows this sort of thing, but the finesse of how you use the response HTML to create the needed session cookie for the next request.

Here's what happens, I send a request, and reading the response, I get and parse out what would become the session cookie if I were to use a Browser. It looks something like this to get my first response:

Expand|Select|Wrap|Line Numbers
  1.  
  2. HttpWebRequest request = (HttpWebRequest)WebRequest.Create( loginUri );
  3.  
  4. request.CookieContainer = new CookieContainer();      
  5.  
  6. string data = "login=" + loginName + "&key=" + password + "&button1=click";
  7. byte[] buffer = Encoding.UTF8.GetBytes( data );
  8.  
  9. // Tell the request that we are going to answer
  10. request.Method = "POST";
  11. request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10";
  12. request.KeepAlive = true;
  13. request.ContentLength = data.Length;
  14.  
  15. // Get the response stream and add our login information
  16. Stream uploadPackage = request.GetRequestStream();
  17. uploadPackage.Write( buffer, 0, buffer.Length );
  18.  
  19. // Send the data by means of .Flush()
  20. uploadPackage.Flush();
  21.  
  22. // Close the stream
  23. uploadPackage.Close();
  24.  
  25. Stream responseStream = response.GetResponseStream();
  26. StreamReader sr = new StreamReader( responseStream );
  27.  
  28. string loginResult = string.Empty;
  29.  
  30. if ( !sr.EndOfStream )
  31.         loginResult = sr.ReadToEnd();
  32.  
  33.  
Parsing the HTML in the loginResult, I can find my not quite created cookie as:

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT type="text/JavaScript">
  2.         document.cookie="KEY=12345; path=/;";
  3.         document.cookie=";";
  4.             if ("".length > 0) {
  5.                 document.cookie=";";
  6.             }
  7.             if ("".length > 0) {
  8.                 document.cookie=";";
  9.             }
  10.             if ("".length > 0) {
  11.                 document.cookie=";";
  12.             }
  13.             if ("".length > 0) {
  14.                 location.assign("");
  15.             }
  16.         </SCRIPT>
  17.  
I understand that a browser will save this out as an HTTPCookie, but What I cannot seem to do is create a cookie for the next request that I need to send to the remote server can or will use with my next call.

Expand|Select|Wrap|Line Numbers
  1. HttpWebRequest uploadRequest =  HttpWebRequest)WebRequest.Create( uploadUri );
  2.  
  3. // put the previous cookie into this request
  4. uploadRequest.CookieContainer = ???????;
  5.  
  6.  
I have tried a dozen ways from Sunday on creating this cookie for my request, but reading the HttpWebResponse result, it tells me I need to log in. Can anyone offer some insight to how this is accomplished?
Sep 24 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.