【问题标题】:HTTPRequest Headers Not in WiresharkHTTPRequest 标头不在 Wireshark 中
【发布时间】:2012-12-07 14:20:42
【问题描述】:

我正在设置一些这样的 HTTP 请求标头:

        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
  • 连接:保持活动状态
  • 用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0)Gecko/20100101 Firefox/17.0
  • 接受:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
  • 接受编码:gzip,deflate,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

知道为什么会这样吗?

【问题讨论】:

    标签: c# http fiddler wireshark webrequest


    【解决方案1】:

    您正尝试在您调用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
    }
    

    (请注意,您确实应该处理响应。)

    【讨论】:

      【解决方案2】:

      是的,您在发送请求后设置标头。

      试试这个:

          //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");
      
          //Added diesposal of response too
          using (WebResponse response = http.GetResponse())
          {
          }
      

      【讨论】:

      • 做到了!非常感谢,贾斯汀!
      猜你喜欢
      • 2019-03-29
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      相关资源
      最近更新 更多