user5204184
user5204184

Reputation: 341

How to get the header values in a GET request?

System.Net.WebRequest req = System.Net.WebRequest.Create(URL);
req.Proxy = null;
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string result = sr.ReadToEnd().Trim();

I have this code which makes a GET request on my URL and returns the JSON data. However, I also need to get the header values from it.

Example output of the URL is the following:

Content-Type: application/json
Content-Language: en
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Vary: Cookie, Accept-Language
Pragma: no-cache
Cache-Control: private, no-cache, no-store, must-revalidate
Set-Cookie: csrftoken=66161e4f97cbf771199ff78cfeea835e; expires=Sat, 20-Feb-2016 06:49:03 GMT; Max-Age=31449600; Path=/
Set-Cookie: mid=VOgqXwABAAHBumwiwEqLc2ScukeD; expires=Fri, 16-Feb-2035 06:49:03 GMT; Max-Age=630720000; Path=/
Connection: close
Content-Length: 108

{"status":"ok","shift":18,"header":"638wprvx7lg5Um0dZzBAKfjIkML12ChQ","edges":100,"iterations":10,"size":42}

Using my code, I can get the last JSON data returned, but I also need the headers. How can I do that? Thanks.

Upvotes: 0

Views: 6332

Answers (2)

sumeet kumar
sumeet kumar

Reputation: 2648

You can use Headers property of HttpRequest Ref

resp.Headers

to get the headers for the response.

Upvotes: 1

Joakim Hansson
Joakim Hansson

Reputation: 544

The HttpWebRequest has a built in property called Headers.

Please have a look at the MSDN page here: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers(v=vs.110).aspx

At the bottom of the page you'll find a very simpe code example that should get you started!

Upvotes: 0

Related Questions