473,327 Members | 2,007 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,327 software developers and data experts.

How to maintain session using HttpWebRequest and SetCookies?

Hi,
I'm working on a windows form based program that can log into a web
service (Apache based, https is used for auth). I was able to post the login
data and obtain a sessionID. However I'm not sure how to maintain this id
over multiple requests. Here are my code:
Globle Varibles
private static int timeOut = 20000;
private CookieContainer cookieContainer; /* other cookies */
private string headerCookie; /* often stores session info */
private string cookieHeader;

1. Login Routine:

public bool OpenLoginSession( string url,
System.Collections.Specialized.NameValueCollection param )
{
WebHeaderCollection headers;
bool statusOK;

HttpWebRequest loginReq =
(HttpWebRequest)WebRequest.Create(url);
cookieContainer = new CookieContainer();
HttpWebResponse loginRes;
//
// Login Request
//
loginReq.CookieContainer = cookieContainer;
loginReq.KeepAlive = false;
loginReq.Method = "POST";
loginReq.AllowAutoRedirect = false;
//
// Build POST stream
//
System.Text.StringBuilder query = new StringBuilder();
query.Append("?");
foreach( string key in param.Keys )
{
query.AppendFormat("{0}={1}&", key, param[key]);
}
byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(
query.ToString() );
Stream os = loginReq.GetRequestStream();
os.Write( buf, 0, buf.Length );
os.Close();
//
// POST the info
//
loginRes = (HttpWebResponse)loginReq.GetResponse();
cookieHeader = cookieContainer.GetCookieHeader(
loginReq.RequestUri );
if ( loginReq.HaveResponse && loginRes.StatusCode ==
HttpStatusCode.OK )
{
headers = loginRes.Headers;
if ( headers["Set-Cookie"] != null )
{
headerCookie = headers["Set-Cookie"];
}

statusOK = true;
}
else
statusOK = false;

loginRes.Close();
return(statusOK);

}

Note, after this routine, "cookieHeader" is an empty string for some reason,
and headerCookie stores the sessionID correctly.

2. Browsing Routine

public void BrowseSecuredUrl( string url )
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse res;

cookieContainer = new CookieContainer();
req.CookieContainer = cookieContainer;
req.KeepAlive = false;
req.Headers.Add("Cookie", headerCookie);

cookieContainer.SetCookies( req.RequestUri ,
cookieHeader );//error here

res = (HttpWebResponse)req.GetResponse();
...
res.Close();

}

The error I'm getting "is An error has occurred when parsing Cookie header
for Uri '...'"

I'm wondering what is the correct way of doing things?

Thanks!
Regards,

Peter
Nov 16 '05 #1
0 9182

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
0
by: Dax Westerman | last post by:
I have a site that I'm trying to migrate to ASP.NET from ASP, and the foremost stumbling block I'm hitting is session state between the ASP and ASP.NET applications. In order to access this...
3
by: Hardik Shah | last post by:
Hi, I am calling an ASP.Net page from an ASP classic application but need the .Net page to have access to ASP classic's session variables. I am using HTTPWebRequest to call ASP classic page...
0
by: paul | last post by:
I must (as a client application) connect via HTTP, authenticate using DIGEST authentication, and then make subsequent HTTP requests. The Problem: If I use System.Net.WebClient or...
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...
4
by: Chris Newby | last post by:
My project currently requires that I integrate an ASP.NET application with an ASP application. One of the issues I'm having is that I have some very long strings being created in an ASP.NET...
3
by: yoni | last post by:
Hi, I am using HttpWebRequest to get the content of a web page. the call to this web page is creating a session on the server. then, I wish to get the content of a 2nd web page on the same site,...
0
by: greenxiar | last post by:
Very Simple Code: CookieContainer cc = new CookieContainer(); cc.SetCookies(new Uri("http://some.com/"), "a=1; b=2; path=/; domain=some.com;"); cc.SetCookies(new Uri("http://some.com/"), "a=1;...
1
by: ALA | last post by:
Hi, does anybody know if it is possible to pass the SessionID with a web request by using a cookie so that the invoked page in the same domain can access the session objects of the current user?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.