4

I'm trying to parse a web page and one of the requirements in the request is setting the appropriate Accept Header. I've verified it doesn't work without setting this via Postman. So I am trying to set the Accept Header in a GET request via HttpWebRequest, but it is not sending over per my findings in Fiddler.

Here is the request when I send via Postman. This is working. Notice the Accept header.

Working Fiddler Request

Working Fiddler Request

Here is the request when I send via code. This is NOT working. See below for my code for this request.

Code Fiddler Request

Code Fiddler Request

Here is my code:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.KeepAlive = true;
req.Accept = @"*text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
//req.Headers.Add("Accept", @"*text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//req.Headers["Accept"] = @*text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
    source = reader.ReadToEnd();
}

I've tried the two lines below the req.Accept that are commented out above as well, but no luck. Any advice appreciated. I also don't mind changing the HttpWebRequest to something else, as long as it works.

aepot
  • 4,558
  • 2
  • 12
  • 24

0 Answers0