How to maintain HttpWebRequest session from scripted cookie...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kgabrys
    New Member
    • Sep 2010
    • 1

    #1

    How to maintain HttpWebRequest session from scripted cookie...

    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:

    Code:
          
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create( loginUri );
    
    request.CookieContainer = new CookieContainer();      
    
    string data = "login=" + loginName + "&key=" + password + "&button1=click";
    byte[] buffer = Encoding.UTF8.GetBytes( data );
    
    // Tell the request that we are going to answer
    request.Method = "POST";
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10";
    request.KeepAlive = true;
    request.ContentLength = data.Length;
    
    // Get the response stream and add our login information
    Stream uploadPackage = request.GetRequestStream();
    uploadPackage.Write( buffer, 0, buffer.Length );
    
    // Send the data by means of .Flush()
    uploadPackage.Flush();
    
    // Close the stream
    uploadPackage.Close();
    
    Stream responseStream = response.GetResponseStream();
    StreamReader sr = new StreamReader( responseStream );
    
    string loginResult = string.Empty;
    
    if ( !sr.EndOfStream )
            loginResult = sr.ReadToEnd();
    Parsing the HTML in the loginResult, I can find my not quite created cookie as:

    Code:
    <SCRIPT type="text/JavaScript">
    		document.cookie="KEY=12345; path=/;";
    		document.cookie=";";
    			if ("".length > 0) {
    				document.cookie=";";
    			}
    			if ("".length > 0) {
    				document.cookie=";";
    			}
    			if ("".length > 0) {
    				document.cookie=";";
    			}
    			if ("".length > 0) {
    				location.assign("");
    			}
    		</SCRIPT>
    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.

    Code:
    HttpWebRequest uploadRequest =  HttpWebRequest)WebRequest.Create( uploadUri );
    
    // put the previous cookie into this request
    uploadRequest.CookieContainer = ???????;
    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?
Working...