Questions tagged [httpwebrequest]

HttpWebRequest is a concrete class for .NET Framework applications that provides an HTTP-specific implementation of the abstract WebRequest class. Related tag: [webrequest].

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object.

The GetResponse method makes a synchronous request to the resource specified in the RequestUri property and returns an HttpWebResponse that contains the response. You can make an asynchronous request to the resource using the BeginGetResponse and EndGetResponse methods.

When you want to send data to the resource, the GetRequestStream method returns a Stream object to use to send data. The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the send data stream.

For client authentication with HttpWebRequest, the client certificate must be installed in the My certificate store of the current user.

The HttpWebRequest class throws a WebException when errors occur while accessing a resource. The WebException.Status property contains a WebExceptionStatus value that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the Response property contains the HttpWebResponse received from the resource.

HttpWebRequest exposes common HTTP header values sent to the Internet resource as properties, set by methods, or set by the system; the following table contains a complete list. You can set other headers in the Headers property as name/value pairs. Note that servers and caches may change or add headers during the request.

You can find the documentation for the class on MSDN.

5585 questions
161
votes
16 answers

How to ignore the certificate check when ssl

I am trying find a way to ignore the certificate check when request a Https resource, so far, I found some helpful article in internet. But I still have some problem. Please review my code. I just don't understand what does the code…
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
158
votes
5 answers

Using CookieContainer with WebClient class

I've previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no built-in method like there is for HttpWebRequests (request.CookieContainer). How…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
142
votes
3 answers

Setting a WebRequest's body data

I'm creating a web request in ASP.NET and I need to add a bunch of data to the body. How do I do that? var request = HttpWebRequest.Create(targetURL); request.Method = "PUT"; response = (HttpWebResponse)request.GetResponse();
William Calleja
  • 4,055
  • 11
  • 41
  • 51
137
votes
2 answers

"NotSupportedException" when WebRequest is unable to find a creator for that prefix

I have a really strange problem with WebRequest in a ServiceStack web application (hosted by XSP on Mono). It seems that the registration of request modules works in a very strange way; I am using WebRequest to create an HTTP request, and it is…
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
137
votes
8 answers

WebClient vs. HttpWebRequest/HttpWebResponse

It seems to me that most of what can be accomplished with HttpWebRequest/Response can also be accomplished with the WebClient class. I read somewhere that WebClient is a high-level wrapper for WebRequest/Response. So far, I can't see anything that…
Dan
  • 11,077
  • 20
  • 84
  • 119
124
votes
4 answers

How do I use WebRequest to access an SSL encrypted site using HTTPS?

I'm writing a program that reads content from a user provided URL. My problem is in the code that goes something like this: Uri uri = new Uri(url); WebRequest webRequest = WebRequest.Create(uri); WebResponse webResponse =…
Alfred B. Thordarson
  • 4,460
  • 8
  • 39
  • 37
124
votes
5 answers

Send JSON via POST in C# and Receive the JSON returned?

This is my first time ever using JSON as well as System.Net and the WebRequest in any of my applications. My application is supposed to send a JSON payload, similar to the one below to an authentication server: { "agent": { …
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
121
votes
18 answers

The server committed a protocol violation. Section=ResponseStatusLine ERROR

I have created a program, tried to post a string on a site and I get this error: "The server committed a protocol violation. Section=ResponseStatusLine" after this line of code: gResponse = (HttpWebResponse)gRequest.GetResponse(); How can I…
manish patel
  • 1,409
  • 3
  • 12
  • 13
115
votes
4 answers

C# HttpWebRequest vs WebRequest

I saw this piece of code: var request = (HttpWebRequest) WebRequest.Create("http://www.google.com"); Why do you need to cast (HttpWebRequest)? Why not just use HttpWebRequest.Create? And why does HttpWebRequest.Create make a WebRequest, not a…
Unknown
  • 45,913
  • 27
  • 138
  • 182
106
votes
4 answers

Add custom header in HttpWebRequest

I need to add some custom headers to the HttpWebRequest object. How can I add Custom Header to HttpWebRequest object in Windows Phone 7.
Nelson T Joseph
  • 2,683
  • 8
  • 39
  • 56
101
votes
6 answers

How to get error information when HttpWebRequest.GetResponse() fails

I am initiating an HttpWebRequest and then retrieving it's response. Occasionally, I get a 500 (or at least 5##) error, but no description. I have control over both endpoints and would like the receiving end to get a little bit more information. …
Trevor
  • 13,085
  • 13
  • 76
  • 99
99
votes
5 answers

Post form data using HttpWebRequest

I want to post some form data to a specified URL that isn't inside my own web application. It has the same domain, such like "domain.client.nl". The web application has a url "web.domain.client.nl" en the url where I want to post to is…
wsplinter
  • 1,021
  • 1
  • 7
  • 7
92
votes
3 answers

Standard for adding multiple values of a single HTTP Header to a request or response

If I want to add a list of values as an HTTP Header, is there a standard way to do this? I couldn't find anything (that I could easily understand) in RFC 822. For example, is comma separated values standard or semi-colon separated values. Is…
jconlin
  • 3,806
  • 6
  • 31
  • 32
91
votes
4 answers

When should one use CONNECT and GET HTTP methods at HTTP Proxy Server?

I'm building a WebClient library. Now I'm implementing a proxy feature, so I am making some research and I saw some code using the CONNECT method to request a URL. But checking it within my web browser, it doesn't use the CONNECT method but calls…
Alexsandro
  • 1,191
  • 1
  • 14
  • 22
90
votes
20 answers

No connection could be made because the target machine actively refused it 127.0.0.1:3446

I'm using the WCF4.0 template -REST. I'm trying to make a method that uploads a file using a stream. The problem always occur at Stream serverStream = request.GetRequestStream(); Class for streaming: namespace LogicClass { public class…
fiberOptics
  • 6,955
  • 25
  • 70
  • 105