0
votes

I'm setting up some HTTP request headers like this:

        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");

I am capturing them like this:

            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);
                }
            }
        }

When I run the testing tool everything seems good:

  • Host: domain.com
  • Connection: Keep-Alive
  • User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0)Gecko/20100101 Firefox/17.0
  • Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
  • Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.9
  • Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

However in Wireshark and Fiddler only the following header is sent:

  • GET / HTTP/1.1
  • Host: domain.com

Any idea why that may be?

2

2 Answers

1
votes

Yes, you are setting the header after you have sent the request.

Try this:

    //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())
    {
    }
3
votes

You're trying to add the headers after you've called http.GetResponse(). That's after the request has been sent. Change it to this:

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
}

(Note that you really should be disposing of the response.)