Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Properties in HttpWebRequest

I am using .Net 4 and VS express 2010.

I could make my post request but I cant set some of the headers. Below code work fines

WebRequest Request = Request.Create("http://example.com") as HttpWebRequest;
Request.Method = "POST";
Request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
Request.Headers.Set("Accept-Encoding", "gzip,  deflate");

The problems is that I cant set other headers like "Accept","UserAgent","Referer","Connection"

I had tried the following ways but fail

Request.Accept = "*/*";
Request.Headers.Set("Accept", "*/*");

For the first line, the Accept property doesn't exist while for the second line, the header need to be edited with a proper method or property.

I am a rookie and I had searched on google and stackoverflow. If you don't know how to solve it, pointing out any direction in fixing this like reinstalling something would be really appreciated.

like image 637
Tai Ming Chan Avatar asked Oct 19 '12 06:10

Tai Ming Chan


1 Answers

Accept doesn't exist as a property on WebRequest, but it does exist on HttpWebRequest.

HttpWebRequest request = (HttpWebRequest) Request.Create("http://example.com");
request.Accept = "*/*";

Even though you were previously using as HttpWebRequest (and I'd strongly suggest preferring casting instead), your variable was declared to be of type WebRequest, which is why it wouldn't compile.

like image 103
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet