WebRequest/WebResponse and Cookie

G

Guest

Hi

I am trying to do very simple http client that requires a login, copy cookie snet by a server and does a request with the cookies. WebRequest->CookieContainer/WebResponse->Cookies does not seems to handle cookie correctly.

User case: If a server send back cookies in headers that looks like following

Set-Cookie: a=assss
Set-Cookie: b=asddas
...

The WebResponse->Cookies only have one value from one Set-Cookie headers. Yes, you can still see all the values in all Set-Cookie in WebRespnose->Headers

Do I miss something

Pete
 
V

vMike

You need a cookie container to accept/pass the cookies. Take a look at the
following snips. You may not need all of this but it should give you the
idea.

mgWebRequest =CType(WebRequest.Create(strWebSite &
strWebPage),HttpWebRequest)
mgWebRequest.timeout = 90000
mgWebRequest.CookieContainer = new cookiecontainer()
mgWebRequest.referer = "whaterrefferer you want"
dim k as int32
Dim siteUri As New Uri(strWebSite)
'this add the current cookies to the container if you need to
for k = 0 to HttpContext.Current.Request.Cookies.count - 1
dim ck as cookie = new cookie()
ck.name = HttpContext.Current.Request.Cookies(k).name
ck.value = HttpContext.Current.Request.Cookies(k).value
ck.domain = strWebSite.substring(strWebSite.indexof(".") + 1)
ck.expires = datetime.now.addyears(50)
mgWebRequest.CookieContainer.add(siteURI,ck)
next k


After you get the response you can use the following to add the receiving
cookies to your cookies

mgWebResponse = CType(mgWebRequest.GetResponse(),HttpWebResponse)

mgWebResponse.cookies =
mgWebrequest.CookieContainer.GetCookies(mgWebrequest.RequestUri)

mgStreamReader = New StreamReader(mgWebResponse.GetResponseStream())
strText = mgStreamReader.ReadToEnd()
dim i as int32

for i = 0 to mgWebResponse.cookies.count - 1

dim hc as httpcookie
hc = new httpcookie(mgWebResponse.cookies.item(i).name)
hc.value = mgWebResponse.cookies.item(i).value
'hc.domain = "if you set this the whole thing will fail. Cannot
setdomain."
hc.expires = datetime.now.addyears(50)
hc.path = mgWebResponse.cookies.item(i).path
HttpContext.Current.Response.AppendCookie(hc)
next i
mgStreamReader.Close()
Peter said:
Hi:

I am trying to do very simple http client that requires a login, copy
cookie snet by a server and does a request with the cookies.
WebRequest->CookieContainer/WebResponse->Cookies does not seems to handle
cookie correctly.
User case: If a server send back cookies in headers that looks like following:

Set-Cookie: a=assssa
Set-Cookie: b=asddasf
...

The WebResponse->Cookies only have one value from one Set-Cookie headers.
Yes, you can still see all the values in all Set-Cookie in
WebRespnose->Headers.
 

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