[英]VB.NET - [HTTP POST] Response.Cookies is only returning first cookie value
I'm using the following code to loop throught the response cookies and save them in a cookiejar. 我正在使用以下代码遍历响应cookie,并将其保存在cookiejar中。
For Each tempCookie As Cookie In Response.Cookies
CookieJar.Add(tempCookie)
MsgBox(tempCookie.ToString)
Next
It happens that it's only returning one cookie, the first one (only runs once in the for): 碰巧它只返回一个cookie,第一个(仅在for中运行一次):
PAGE_VIS=A|186.213.98.144|1381089446.485193|
Response.Cookies has only one cookie on it, but Response.Headers has all of them. Response.Cookies上只有一个cookie,但是Response.Headers拥有所有cookie。
How could I parse them from the Response.Headers and add the on cookiecontainer to use them later? 我如何从Response.Headers中解析它们并添加on cookiecontainer以便以后使用? The cookies in Response.Headers are here:
Response.Headers中的cookie在这里:
Set-Cookie: PAGE_VIS=A|186.213.98.144|1381089446.485193|; domain=PAGE; path=/; expires=Mon, 07-Oct-2013 06:00:00 GMT,CSASF=;Version=1;Domain=PAGE;Path=/;Max-Age=7776000,CSAPAGES=ekpEOWluWTZSMXpDSWRMMm8wWWZ3UT09|5749|kNm6h4hlOB4SwLFg2gOIMXXpmTXDD5tnvB/dJ7AS3mUqUiLtpHLxzsWjfKsI1rcq6fp/DKre3QHAZHPQi968iw==;Version=1;Domain=PAGE;Path=/;Max-Age=7776000;HttpOnly,NAM_LOGIN_DEFAULT=user;Version=1;Domain=PAGE;Path=/;Max-Age=129600,UPL09=2-25|39|54|56|58|90|91|105|106|156|182|212|218|219|242|245|248|284|285|480|1222;Version=1;Domain=PAGE;Path=/;Max-Age=2592000,PAGE_OTR=;Version=1;Domain=PAGE;Path=/;Max-Age=2592000,CAUBR01=5eeOUuDdqyj+ILCQ7oOg71yXEgIGi2KJ7bjDiTt27tTXgjT7jqC49eGprgjZ+8rQ7O7mqG/qEq28u4EXAiEbfE0qqI9QqUrj27LrEAi4kSZacSSTAo5YY+yQZYFr4qGgCp18odS/Y92mkEtqEuCoxuG+qsK5fhOJmKQqqpxqE07ORqEn2JGEhOHVmoQqq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obqMesqECkKqMCD7GLb8ZKxKoqqxVmqKwqGqqM/QKqq9ZEqowesKMqkZKqEk7x0bGreJGKlvKVmoqqkZCsEqJKbqZuViKZDwCMqH7mqGMmKqboQeEtM0GZHKMqqw9kqKxqZsqM7VKsq9GGoCx7mKbqsGKqZmewdMZV7VEKeMK/kCqqmKosGqVK0qZseQKGqxobqMesqKSkKqMC97GOuBZEkKKqqxVmqKvqGqqMe7KEqDZKDovesKMqkZKqZk7x6uGEedGKtwKVmoqqkZCsKquKjqGHVtKZTvCMq97kqGMmKquoNeKJMrGZXKMqqvrkqKxqZsqxEVKqqFKGQCx7mKuqsGKqZmevrMZm7wKKdMKVkCqqmEosGqVKLqZecvKGNxouqMesqKtkKqMCv7GbJjZZ1KOqqxVmqK8qGqqMlqKyqLZGIo8esKMqqZKqGk7xFJGuetGK/rKVmoqqkZCsGqbKsqKKVsKZr8CMq87kqGMmKqJoteGYMqGZGKMqq8CkqKxqZsqnzVKdq3EGZCx7mKJqsGKqZme8xMZQ7TGKsMKMkCqqmZosGqVKDqZFThKGxxoJqMesqG4kKqMCo7G7C0ZGFKCqqxVmqK8qGsqM72KSqiZZwo9esKMqqZKqKm7mGVC;Version=1;Domain=PAGE;Path=/
Why it isn't getting all the rest of the cookies, only the first one? 为什么它没有获得所有剩余的Cookie,只有第一个?
How do I solve that? 我该如何解决?
The entire function in here: 整个功能在这里:
Private Function LogonPage(ByVal URL As String, ByRef CookieJar As CookieContainer, ByVal PostData As String)
Dim reader As StreamReader
Dim Request As HttpWebRequest = HttpWebRequest.Create(URL)
Request.Method = "POST"
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Request.CookieContainer = CookieJar
Request.AllowAutoRedirect = False
Request.ContentType = "application/x-www-form-urlencoded"
Request.ContentLength = PostData.Length
Dim requestStream As Stream = Request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(PostData)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
Dim Response As HttpWebResponse = Request.GetResponse()
For Each tempCookie As Cookie In Response.Cookies
CookieJar.Add(tempCookie)
Next
reader = New StreamReader(Response.GetResponseStream())
Return reader.ReadToEnd()
Response.Close()
End Function
Could you tell me what I'm doing wrong, and if it's possible, point me to the solution? 您能告诉我我做错了什么吗,如果可能,请指出解决方案? Thank you very much.
非常感谢你。
In case of multiple cookies, Set-Cookie
header might contain cookies separated by comma ,
while cookie properties are separated by semicolon ;
如果有多个Cookie,则
Set-Cookie
标头可能包含以逗号分隔的Cookie ,
而cookie属性则以分号分隔;
and Response.Headers("Set-Cookie")
returns all cookies in a string
. 和
Response.Headers("Set-Cookie")
返回string
所有cookie。
Response.Headers("Set-Cookie")
Response.Headers("Set-Cookie")
上应用cookie分离技术 Cookie
objects with this information Cookie
对象 CookieContainer
. CookieContainer
。 Note that splitting cookies on comma ,
is not useful because any cookie property like expires might also contain comma in it like expires=Mon, 07-Oct-2013 06:00:00 GMT : 需要注意的是逗号分割的cookies
,
是没有用的,因为任何cookie的财产像过期还可能包含逗号它喜欢到期=周一,07-OCT-2013 06:00:00 GMT:
Dim strCookies As String = Response.Headers("Set-Cookie")
' Splitting on comma is just for example
Dim cookies() As String = strCookies.Split(commaDelimeter, StringSplitOptions.RemoveEmptyEntries)
For Each ck As String In cookies
Dim ckProp() As String = ck.Split(semicolonDelimeter, StringSplitOptions.RemoveEmptyEntries)
Dim cookie As New Cookie(ckProp(0), ckProp(1))
CookieJar.Add(cookie)
Next ck
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.