1

我正在使用以下代码循环响应 cookie 并将它们保存在 cookiejar 中。

    For Each tempCookie As Cookie In Response.Cookies
        CookieJar.Add(tempCookie)
        MsgBox(tempCookie.ToString)
    Next

碰巧它只返回一个cookie,第一个(只在for中运行一次):

 PAGE_VIS=A|186.213.98.144|1381089446.485193|

Response.Cookies 上只有一个 cookie,但 Response.Headers 拥有所有这些。

我如何从 Response.Headers 中解析它们并添加 on cookiecontainer 以便以后使用它们?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+qsK5fhOJmKQqqpxqE07ORqEn2JGEhOHVmoQqqKCqEq78+2EMqtKZlrvMqv2qmKMqEqqOIqZ72NKGRxO2mKeskKwkZq2CH7GrC+GGoKOCqOVmqEOqEkbr82EjqcGKVHoQqn2qkEMqKk2xLqEk20EEtSr2qo2kqErqEqqODqZh7YKGtxK2mCesmEMsGqVS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sGKqZmewdMZV7VEKeMK/kCqqmKosGqVK0qZseQKGqxobqMesqKSkKqMC97GOuBZEkKKqqxVmqKvqGqqMe7KEqDZKDovesKMqkZKqZk7x6uGEedGKtwKVmoqqkZCsKquKjqGHVtKZTvCMq97kqGMmKquoNeKJMrGZXKMqqvrkqKxqZsqxEVKqqFKGQCx7mKuqsGKqZmevrMZm7wKKdMKVkCqqmEosGqVKLqZecvKGNxouqMesqKtkKqMCv7GbJjZZ1KOqqxVmqK8qGqqMlqKyqLZGIo8esKMqqZKqGk7xFJGuetGK/rKVmoqqkZCsGqbKsqKKVsKZr8CMq87kqGMmKqJoteGYMqGZGKMqq8CkqKxqZsqnzVKdq3EGZCx7mKJqsGKqZme8xMZQ7TGKsMKMkCqqmZosGqVKDqZFThKGxxoJqMesqG4kKqMCo7G7C0ZGFKCqqxVmqK8qGsqM72KSqiZZwo9esKMqqZKqKm7mGVC;Version=1;Domain=PAGE;Path=/


为什么它没有得到所有其余的饼干,只有第一个?
我该如何解决?

这里的整个功能:

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


你能告诉我我做错了什么,如果可能的话,请指出解决方案?非常感谢你。

4

1 回答 1

0

In case of multiple cookies, Set-Cookie header might contain cookies separated by comma , while cookie properties are separated by semicolon ; and Response.Headers("Set-Cookie") returns all cookies in a string.

  1. Apply cookie separation technique on Response.Headers("Set-Cookie")
  2. Extract each cookie's name, value, path, domain etc.
  3. Create Cookie objects with this information
  4. Add cookies to 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:

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
于 2013-10-06T21:58:45.773 回答