473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

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
0 1369

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

Similar topics

0
by: sandyde2 | last post by:
Hi all, b4, i only use session. i dunno whether it is good or not, i do not use the session control organization provided by PHP but developed an individule session database table because i...
7
by: ehendrikd | last post by:
hi all i need some clarification on how the php session work in relation to cookies. we have a web site where users need to log in. a few of our users were having troubles with their browser...
1
by: Craig | last post by:
I wrote a generic page to do HttpWebRequest operations based on 3 querystring parameters for the uri, username, and password for basic authentication pages. The page is designed to help with...
3
by: Eþref DURNA | last post by:
My Aim is to make a windows application download which support session and cookies. There is a web site which gives images as links like this...
3
by: Amil | last post by:
Please don't repond to this if you are guessing or just don't know the answer. I'm trying to login to a backend system running Java/Tomcat. I create a HttpWebRequest with the login data and do...
1
by: Manuel | last post by:
I thought this would be simple: In my VB.NET application, make and instance of a web control, post a username/password to a website, keep "browsing" the website with the control, extracting...
2
by: Xpou | last post by:
Hi, I have a windows form app which makes post httpwebrequests to the same pages of the same web site several times. This web site generates a "phpsessid" which is a session id residing in...
1
by: spitapps | last post by:
I want to have sessions on my website, this is my web.config file: <configuration> <appSettings/> <connectionStrings/> <system.web> <sessionState mode="InProc" cookieless="UseCookies"...
1
by: webdevaccount | last post by:
Hi, I am trying to deceive whether I should use Asp.net 2.0's FormAuthentication (cookie) or the Session object to store the login ID of a given user. Question 1: Assuming that the app will...
2
by: anjaligoel | last post by:
Good morning for everyone please anyone tell me tht how can i maintain the session through javascript i found that we can use cookies in javascript but can we maintain session in it. currently...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.