2012-12-07 65 views
0

我設置了一些HTTP請求頭是這樣的:的HTTPRequest頭不在Wireshark的

 this.Url = new Uri(u); 
     HttpWebRequest http = (HttpWebRequest)WebRequest.Create(Url); 
     WebResponse response = http.GetResponse(); 

     //headers 
     http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0\r\n"; 
     http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; 
     http.Headers.Add("Accept-Encoding", "gzip,deflate,sdch'r'n"); 
     http.Headers.Add("Accept-Language", "en-US,en;q=0.9\r\n"); 
     http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"); 

我捕捉他們是這樣的:

  for (count = 0; count < http.Headers.Keys.Count; count++) 
     { 
      headerKey = http.Headers.Keys[count]; 
      headerValue = http.Headers[headerKey]; 

      if (headerValue != null) 
      { 
       if (headerKey == null) 
       { 
        requestbuffer.Append(headerValue); 
        requestbuffer.Append(Newline); 
       } 
       else 
       { 
        requestbuffer.Append(headerKey + ": " + headerValue); 
        requestbuffer.Append(Newline); 
       } 
      } 
     } 

當我運行測試工具,一切似乎都很好:

  • 主持人:domain.com
  • 連接:保持活動
  • User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; RV:17.0)壁虎/ 20100101火狐/ 17.0
  • 接受:text/html的,應用/ XHTML + xml的,應用/ XML; Q = 0.9,/; Q = 0.8
  • 接受編碼:gzip,放氣,SDCH接受語言:EN-US,EN; q = 0.9
  • 接收字符集:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3

然而在Wireshark的和Fiddler只發送以下標題:

  • GET/HTTP/1.1
  • 主機:domain.com

任何想法爲什麼這可能是?

回答

0

是的,你要設置頁眉您發送請求後。

試試這個:

//headers 
    http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0\r\n"; 
    http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; 
    http.Headers.Add("Accept-Encoding", "gzip,deflate,sdch'r'n"); 
    http.Headers.Add("Accept-Language", "en-US,en;q=0.9\r\n"); 
    http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"); 

    WebResponse response = http.GetResponse(); 
+0

這做到了!非常感謝,賈斯汀! –

3

您正在嘗試在之後添加標頭,您稱爲http.GetResponse()。在之後,這是請求已發送。它改成這樣:

this.Url = new Uri(u); 
HttpWebRequest http = (HttpWebRequest)WebRequest.Create(Url); 

//headers 
http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0\r\n"; 
http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; 
http.Headers.Add("Accept-Encoding", "gzip,deflate,sdch'r'n"); 
http.Headers.Add("Accept-Language", "en-US,en;q=0.9\r\n"); 
http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"); 

using (WebResponse response = http.GetResponse()) 
{ 
    // Do whatever 
} 

(請注意,你真的應該處置的響應。)