HttpWebRequest, Session Cookie and Windows Forms app


G

Guest

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 memory (no file is stored). It is a
parameter of the url of the second page :

Page1 : http://www.mydomain.com/mystartpage.php
Page2 : http;//www.mydomain.com/mysecondpage.php;phsessid=abed12ee65f...

My problem is : the cookie seems to stay in memory and I always get the same
session id where I would like to get a new one each time. I have spent hours
reading posts and searching the web and could not find a smarter solution
than killing the web session via the wininet API (!).

I must be doing something wrong... Some help would be greatly appreciated :)

---
here is the (extract of) code :

myReq = DirectCast(WebRequest.Create(url), HttpWebRequest)
myReq.Timeout = m_Timeout
myReq.Referer = m_Referer
myReq.AllowAutoRedirect = True
myReq.CachePolicy = New
Cache.HttpRequestCachePolicy(Cache.HttpRequestCacheLevel.NoCacheNoStore)
myReq.CookieContainer = New CookieContainer
myReq.Method = "POST"
myReq.Accept = "*/*"
myReq.ContentType = m_ContentType
myReq.UserAgent = m_useragent
myReq.KeepAlive = False
'oCookies is a CookieCollection reset to nothing before each new set of
requests
If Not oCookies Is Nothing AndAlso oCookies.Count > 0 Then
myReq.CookieContainer.Add(oCookies)
End If

If postdata <> "" Then
byteArray = encodasc.GetBytes(postdata)
myReq.ContentLength = byteArray.Length
newStream = myReq.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
newStream.Close()
End If

Try
myRep = myReq.GetResponse
myReq = Nothing

Catch exwe As Net.WebException
Return nothing

End Try

Dim enc As Encoding
enc = Encoding.GetEncoding(1252)
If myRep.ContentEncoding.Length > 0 Then
enc = Encoding.GetEncoding(myRep.ContentEncoding)
End If

Dim myRepStream As New StreamReader(myRep.GetResponseStream(), enc)
Dim myHTMLSourceCode As String = myRepStream.ReadToEnd
myRepStream.Close()

'update my headers vars
'...

If myRep.Cookies.Count > 0 Then
If oCookies Is Nothing Then
oCookies = myRep.Cookies
Else
Dim oRespCookie As Cookie, oReqCookie As Cookie
For Each oRespCookie In myRep.Cookies
Dim foundCookie As Boolean = False
For Each oReqCookie In oCookies
If oReqCookie.Name = oRespCookie.Name Then
oReqCookie.Value = oRespCookie.Value
foundCookie = True
Exit For
End If
Next
If Not foundCookie Then oCookies.Add(oRespCookie)
Next
End If
End If

myRep.Close()
Return myHTMLSourceCode
 
Ad

Advertisements

C

Cor Ligthert

Xpou,

I did a while nothing with cookies, and I see seldom those questions
answered in this newsgroup.

I think that you have a better changes in
microsoft.public.dotnet.framework.aspnet
or
microsoft.public.dotnet.framework
or
microsoft.public.dotnet.general

I would in your situation crosspost it to those newsgroup (one message in
one time to all those three)

However you are of course welcome here and don't forget to check this
newsgroup as well on answers.

I hope this helps anyway.

Cor
 
Ad

Advertisements

G

Guest

Cor,

I will follow your suggestion next time. I found the bug actually and it is
not in this code. I thought I was setting oCookies to nothing before each
session but I was not.

Thanks for your reply,
Xpou
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top