G
Guest
I'm now writing a small program to communicate a web server to simulate a web
client. I use te httpwebrequest to talk with the server, and it works find
for "POST" method, however, when i test other link using "GET" method, i
found that the cookies data has not included in the request.
Here is the sample:
' sURL is the URL of server page
' pCookies is a varible contain the cookies data
'
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
myHttpWebRequest.UserAgent = _HttpUserAgent
myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
myHttpWebRequest.CookieContainer.Add(pCookies)
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
I find that the web server cannot obtain the cookies data in this case, and
I have capture the packing from my pc, i find that the packet of "HTTP GET"
does not contain the cookies data.
However, if I change the program to use "POST" method with some dummy data,
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
myHttpWebRequest.UserAgent = _HttpUserAgent
myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
myHttpWebRequest.CookieContainer.Add(pCookies)
Dim sData As String = "a=1"
myHttpWebRequest.Method = "POST"
myHttpWebRequest.ContentLength = sData.Length
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim myStreamWriter As StreamWriter = Nothing
myStreamWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())
myStreamWriter.Write(psPostData)
myStreamWriter.Close()
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
It works fine, and I capture the packet again, I find that the required
cookie is included in the "HTTP POST" packet.
In fact, I'm not sure how to use the cookies in this case, I just modify the
code from books which does not include cookies, and I combine the example for
using cookies with "POST" method. So, I'd like to know if I have missed some
step for a "Get" method with cookies.
BTW, may I know if there has any example on using the Cookies with
httpwebrequest using "GET" method?
Thanks in advance!
client. I use te httpwebrequest to talk with the server, and it works find
for "POST" method, however, when i test other link using "GET" method, i
found that the cookies data has not included in the request.
Here is the sample:
' sURL is the URL of server page
' pCookies is a varible contain the cookies data
'
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
myHttpWebRequest.UserAgent = _HttpUserAgent
myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
myHttpWebRequest.CookieContainer.Add(pCookies)
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
I find that the web server cannot obtain the cookies data in this case, and
I have capture the packing from my pc, i find that the packet of "HTTP GET"
does not contain the cookies data.
However, if I change the program to use "POST" method with some dummy data,
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
myHttpWebRequest.UserAgent = _HttpUserAgent
myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
myHttpWebRequest.CookieContainer.Add(pCookies)
Dim sData As String = "a=1"
myHttpWebRequest.Method = "POST"
myHttpWebRequest.ContentLength = sData.Length
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim myStreamWriter As StreamWriter = Nothing
myStreamWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())
myStreamWriter.Write(psPostData)
myStreamWriter.Close()
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
It works fine, and I capture the packet again, I find that the required
cookie is included in the "HTTP POST" packet.
In fact, I'm not sure how to use the cookies in this case, I just modify the
code from books which does not include cookies, and I combine the example for
using cookies with "POST" method. So, I'd like to know if I have missed some
step for a "Get" method with cookies.
BTW, may I know if there has any example on using the Cookies with
httpwebrequest using "GET" method?
Thanks in advance!