2013-08-26 103 views
1

我目前正在研究一個可以幫助我用cookies和POST參數創建WebRequest的類。但response.cookies總是空的。我如何獲取返回的cookie並將它們發送給下一個請求?WebResponse不返回任何cookies

class WebHandler 
{ 
    private string lasturl = ""; 
    private Dictionary<string, Cookie> cookies; 
    public string lastContent; 

    public WebHandler() 
    { 
     cookies = new Dictionary<string, Cookie>(); 
    } 

    public HttpWebResponse request(string address) 
    { 
     lasturl = address; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); 
     foreach (KeyValuePair<string, Cookie> pair in this.cookies) 
     { 
      Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value); 
      request.CookieContainer.Add(pair.Value); 
     } 
     request.Method = "GET"; 
     if(lasturl != "") 
      request.Referer = lasturl; 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     foreach (Cookie newCookie in response.Cookies) 
     { 
      Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value); 
      this.cookies[newCookie.Name] = newCookie; 
     } 
     lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
     return response; 
    } 

    public HttpWebResponse request(string address, Dictionary<string, string> postParameters) 
    { 
     lasturl = address; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); 
     foreach (KeyValuePair<string, Cookie> pair in this.cookies) 
     { 
      Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value); 
      request.CookieContainer.Add(pair.Value); 
     } 
     request.Method = "POST"; 

     string postData = ""; 
     foreach (string key in postParameters.Keys) 
      postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postParameters[key]) + "&"; 


     byte[] data = Encoding.ASCII.GetBytes(postData); 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = data.Length; 
     Stream requestStream = request.GetRequestStream(); 
     requestStream.Write(data, 0, data.Length); 
     requestStream.Close(); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     foreach (Cookie newCookie in response.Cookies) 
     { 
      Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value); 
      this.cookies[newCookie.Name] = newCookie; 
     } 
     lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
     return response; 
    } 
} 
+0

如果有一個重定向然後是,你不會得到任何cookie,試圖解析響應報頭和u會看到cookies, – K3rnel31

回答

1

如果HttpWebResponse對象的Cookies集合爲空,則意味着響應沒有新的cookies。要訪問請求和響應的所有Cookie,然後讀取來自HttpWebRequest對象餅乾代替,就像這樣:

foreach (Cookie newCookie in request.Cookies) 
{ 
    // Do something with cookies here for next request 
} 
+0

我以爲我會在響應中獲得所有的cookie,並且必須將相同的cookie放入下一個請求中?你的例子會在請求中提供所有的cookies(這意味着沒有cookies,因爲沒有人設置它們)。我想保留舊的響應中的cookie,並將其添加到下一個請求中! –

+0

那麼,如果您的請求中沒有Cookie,並且響應中沒有Cookie,那麼您將沒有Cookie。 –

4

由於this.cookies最初爲null,您的代碼將不會進入循環

foreach (KeyValuePair<string, Cookie> pair in this.cookies) 
{ 
    Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value); 
    request.CookieContainer.Add(pair.Value); 
} 

不好的一部分,你永遠不會看到request.CookieContainer爲空(否則你會得到空引用異常),這會告訴你什麼是錯的。

由於request.CookieContainer爲空,HttpWebResponse從不會獲取返回的cookie。

解決方案:簡單,只需添加

if (request.CookieContainer == null) 
     request.CookieContainer = new CookieContainer(); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); 

PS:你可能需要閱讀this question約在C#中的命名約定。

PS2:在使用變量名request不提高可讀性的方法request

+0

但現在我可以接收cookie並添加到下一個請求中,但是我的php腳本沒有看到任何cookie(但在瀏覽器中一切正常)。我可能需要做域名事情嗎? –

+0

非常奇怪的行爲......響應對象有一個非null的cookie容器,但沒有cookie。這絕對有效。 –

2

我有同樣的問題,所以我加了request.AllowAutoRedirect=false;,我可以讓餅乾:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); 
... 
request.Method = "POST"; 
request.AllowAutoRedirect=false; 
+0

謝謝!瘋狂。 – Stephen