Persistemt https session?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I am trying to connect to an https server and download a file. The server
requires a client side certificate, which I have loaded successfully using
the X509Certificate class. I then add this certificate to the HttpWebRequest
class and connect to the server.

The problem arises because the initial request to the server gets a 302
redirect response. This response is OK, and is also recieved in IE7. The
second request in IE7 is not redirected, and I can download the file
successfully. However, if I try this from my C# client I get the redirect on
the first and second request. Is the SSL connection being renegotiated at
every request?

Code:
static HttpWebRequest GetHttpRequest()
{
X509Certificate cert = new X509Certificate(@"c:\cert.P12", "somePassword");
string url = "https://www.cdinternet.com/Reports/someTextFile.TXT";
Uri clearstream = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(clearstream);
request.ClientCertificates.Add(cert);
request.Proxy = GetProxy();
request.AllowAutoRedirect = false;
request.ProtocolVersion = HttpVersion.Version10;
request.KeepAlive = true;
HttpRequestCachePolicy noCachePolicy = new
HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
CLR 1.1.4322; .NET CLR 2.0.50727)";
request.PreAuthenticate = false;
request.Pipelined = true;
return request;
}

I am calling this method by:
request = GetHttpRequest();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
..... process stream.....
.... repeat the request...
request = GetHttpRequest();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
..... process stream.....

Any advice would be welcome. If I have left out any necessary detail please
let me know.

Thanks,
David.
 
This is the reply I get from the server:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A HREF=" / " >here</A>.<P>
</BODY></HTML>

If I set the AllowAutoRedirect propert on the HttpWebRequest to true, the
app just hangs when doing the request.GetResponse().

As mentioned before I get this response every time the request is made. IE7
redirects the first time the page is requested, but gets the document on the
second request.

Thanks in advance!
 
Problem solved :-)
The server was sending back a cookie as part of the response. To fix the
problem all I had to do was set this cookie value on the second request
object. Voila!

Hope this is useful to someone in the future....

Many Thanks,
David.
 
Back
Top