2009-01-14 71 views
1

我相信我一定在做錯事。但不能爲我的生活找出發生了什麼。HttpWebRequest Cookie奇怪

我有一個問題,似乎框架中的HttpWebRequest類不能正確解析來自Web響應的Cookie。

我使用招,看看是怎麼回事,併發出請求後,響應的頭看起來像這樣:

HTTP/1.1 200 Ok 
Connection: close 
Date: Wed, 14 Jan 2009 18:20:31 GMT 
Server: Microsoft-IIS/6.0 
P3P: policyref="/w3c/p3p.xml", CP="CAO DSP IND COR ADM CONo CUR CUSi DEV PSA PSD DELi OUR COM NAV PHY ONL PUR UNI" 
Set-Cookie: user=v.5,0,EX01E508801E$97$2E401000t$1BV6$A1$EC$104$A1$EC$104$A1$EC$104$21O001000$1E31!90$7CP$AE$3F$F3$D8$19o$BC$1Cd$23; Domain=.thedomain.com; path=/ 
Set-Cookie: minfo=v.4,EX019ECD28D6k$A3$CA$0C$CE$A2$D6$AD$D4!2$8A$EF$E8n$91$96$E1$D7$C8$0F$98$AA$ED$DC$40V$AB$9C$C1$9CF$C9$C1zIF$3A$93$C6$A7$DF$A1$7E$A7$A1$A8$BD$A6$94c$D5$E8$2F$F4$AF$A2$DF$80$89$BA$BBd$F6$2C$B6$A8; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Set-Cookie: accttype=v.2,3,1,EX017E651B09k$A3$CA$0C$DB$A2$CB$AD$D9$8A$8C$EF$E8t$91$90$E1$DC$C89$98$AA$E0$DC$40O$A8$A4$C1$9C; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Set-Cookie: tpid=v.1,20001; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Set-Cookie: MC1=GUID=541977e04a341a2a4f4cdaaf49615487; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Set-Cookie: linfo=v.4,EQC|0|0|255|1|0||||||||0|0|0||0|0|0|-1|-1; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Set-Cookie: group=v.1,0; expires=Sunday, 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Content-Type: text/html 

但是,當我看着response.Cookies,我看得遠我期待的更多cookie,不同cookie的值被分成不同的cookie。

手動得到標題似乎導致更多wierdness

例如:代碼

foreach(string cookie in response.Headers.GetValues("Set-Cookie")) 
{ 
    Console.WriteLine("Cookie found: " + cookie); 
} 

產生輸出:

Cookie found: user=v.5 
Cookie found: 0 
Cookie found: EX01E508801E$97$2E401000t$1BV6$A1$EC$104$A1$EC$104$A1$EC$104$21O00 
1000$1E31!90$7CP$AE$3F$F3$D8$19o$BC$1Cd$23; Domain=.thedomain.com; path=/ 
Cookie found: minfo=v.4 
Cookie found: EX019ECD28D6k$A3$CA$0C$CE$A2$D6$AD$D4!2$8A$EF$E8n$91$96$E1$D7$C8$0 
F$98$AA$ED$DC$40V$AB$9C$C1$9CF$C9$C1zIF$3A$93$C6$A7$DF$A1$7E$A7$A1$A8$BD$A6$94c$ 
D5$E8$2F$F4$AF$A2$DF$80$89$BA$BBd$F6$2C$B6$A8; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Cookie found: accttype=v.2 
Cookie found: 3 
Cookie found: 1 
Cookie found: EX017E651B09k$A3$CA$0C$DB$A2$CB$AD$D9$8A$8C$EF$E8t$91$90$E1$DC$C89 
$98$AA$E0$DC$40O$A8$A4$C1$9C; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Cookie found: tpid=v.1 
Cookie found: 20001; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Cookie found: MC1=GUID=541977e04a341a2a4f4cdaaf49615487; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Cookie found: linfo=v.4 
Cookie found: EQC|0|0|255|1|0||||||||0|0|0||0|0|0|-1|-1; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 
Cookie found: group=v.1 
Cookie found: 0; expires=Sunday 
Cookie found: 31-Dec-2014 23:59:59 GMT; Domain=.thedomain.com; path=/ 

,你可以看到 - 在第一個Cookie list raw response:

  1. 設置Cookie:用戶= v.5,0,EX01E508801

是越來越分成:

  1. cookie中:用戶= 5節
  2. 發現
  3. 的Cookie:0
  4. 曲奇發現:EX01E508801E $ ..........

那麼 - 這是怎麼回事?

  • 我錯了嗎?
  • HttpWebRequest類是否錯誤地解析了http頭文件?
  • 它吐出產生無效HTTP頭的請求的網絡服務器?
+0

這是expedia? – 2014-08-27 16:39:13

回答

2

要避免cookie值中的逗號 - 您可能需要對它們進行不同的編碼。

原來netscape spec餅乾是在這個問題上有點含糊,稱

NAME = VALUE: 此字符串是不包括分號,逗號 和空格字符序列。如果需要在名稱或值 中放置這樣的數據,則建議使用某種編碼方法,例如URL風格 %XX編碼,儘管沒有定義或要求編碼 。

推薦,但不是要求

0

看來,服務器發送不正確的cookie值。恕我直言逗號不允許在Cookie值中使用。他們需要用%2C編碼。

0

您可能想要使用GetValues(i)的整數重載;你應該只回7串然後