473,386 Members | 1,743 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,386 software developers and data experts.

HttpWebRequest, Session Cookie and Windows Forms app

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.HttpRequestCach eLevel.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

--
Xpou
Nov 21 '05 #1
2 7045
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

"Xpou" <Xp**@discussions.microsoft.com>
....
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.HttpRequestCach eLevel.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

--
Xpou

Nov 21 '05 #2
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

"Cor Ligthert" wrote:
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


Nov 21 '05 #3

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

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
0
by: Peter Qian | last post by:
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...
9
by: buran | last post by:
Dear ASP.NET Programmers, How can I post data to an ASP.NET login page and pass authentication? The login page uses forms authentication, users must supply usernames and password and have to...
2
by: Keith Patrick | last post by:
I'm trying to programmatically post data to another page within my ASP.Net app. Not POSTing is not an option (I can't store this data in my session, context, hidden fields, or anything else...I've...
11
by: Keith Patrick | last post by:
Could someone explain to me the relationship between these two classes? I am ripping my hair out trying to divert an HttpRequest to a new location via an HttpWebRequest, but I cannot get my...
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: davvel | last post by:
It has been 6 days of re-writing the same code in different ways to try and avoid a getResponse Timeout which haunted me for much too long now. I am trying to do a very simple thing which is...
16
by: Cheung, Jeffrey Jing-Yen | last post by:
I have a windows form application that generates a request, downloads an image, and waits the user to enter in login info. Unfortunately, this image is dynamic and based on session data. I have...
10
by: rlueneberg | last post by:
I am trying to foward the old sessionID using "Session.SessionID" to an HttpWebRequest CookieContainer so that I can capture the requested page session variables but it is not working as it is...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.