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
87
votes
10 answers

Using a self-signed certificate with .NET's HttpWebRequest/Response

I'm trying to connect to an API that uses a self-signed SSL certificate. I'm doing so using .NET's HttpWebRequest and HttpWebResponse objects. And I'm getting an exception that: The underlying connection was closed: Could not establish trust…
Dominic Scheirlinck
  • 2,627
  • 2
  • 23
  • 28
84
votes
4 answers

How to make my web scraper log in to this website via C#

I have an application that reads parts of the source code on a website. That all works; but the problem is that the page in question requires the user to be logged in to access this source code. What my program needs a way to initially log the user…
Dillon
83
votes
8 answers

How do I see the raw HTTP request that the HttpWebRequest class sends?

I know you are all going to answer "use a debugging proxy server like Fiddler" but it's not that simple. Here's my situation: I have some code that runs on a server, in an ASP.NET page code-behind (aspx.cs), which (among other things) establishes a…
eeeeaaii
  • 3,372
  • 5
  • 30
  • 36
83
votes
15 answers

The request was aborted: Could not create SSL/TLS secure channel

My customer has informed my of issues with their SSL and Internet Explorer. They said they get trust issues when accessing the URL. I am accessing JSON through HTTPS. The website sits on one server and I am using the console app on my local…
Joseph Anderson
  • 4,114
  • 4
  • 45
  • 99
83
votes
14 answers

How secure is a HTTP POST?

Is a POST secure enough to send login credentials over? Or is an SSL connection a must?
Matt
  • 5,249
  • 12
  • 40
  • 45
81
votes
9 answers

In c# convert anonymous type into key/value array?

I have the following anonymous type: new {data1 = "test1", data2 = "sam", data3 = "bob"} I need a method that will take this in, and output key value pairs in an array or dictionary. My goal is to use this as post data in an HttpRequest so i will…
Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
79
votes
4 answers

How can you add a Certificate to WebClient (C#)?

I know it is pretty simple to add a certificate to a HttpWebRequest. However, I have not found a way to do the equivalent using WebClient. Basically, I want to send out a POST with a specific certificate using WebClient. How would you accomplish…
Andrew
  • 3,524
  • 5
  • 35
  • 50
76
votes
15 answers

HttpWebRequest is extremely slow!

I am using an open source library to connect to my webserver. I was concerned that the webserver was going extremely slow and then I tried doing a simple test in Ruby and I got these results Ruby program: 2.11seconds for 10 HTTP GETs Ruby…
Earlz
  • 62,085
  • 98
  • 303
  • 499
75
votes
6 answers

How to make an HTTP get request with parameters

Is it possible to pass parameters with an HTTP get request? If so, how should I then do it? I have found an HTTP post requst (link). In that example the string postData is sent to a webserver. I would like to do the same using get instead. Google…
CruelIO
  • 18,196
  • 16
  • 40
  • 58
67
votes
4 answers

Max number of concurrent HttpWebRequests

I'm stress testing a web app and have set up a windows test program that spins up a number of threads and sends out a web request on each one. Problem is I get the following output: 01/09/09 11:34:04 Starting new HTTP request on 10 01/09/09 11:34:04…
Duncan
  • 10,218
  • 14
  • 64
  • 96
65
votes
10 answers

Is that possible to send HttpWebRequest using TLS1.2 on .NET 4.0 framework

My application connects to Experian server and Experian will soon stop supporting TLS 1.0 and TLS 1.1. All connectivity using HTTPS must use TLS Version 1.2. I want to do some research on that issue and see sending HttpWebRequest using TLS 1.2 on…
gene
  • 2,098
  • 7
  • 40
  • 98
57
votes
5 answers

Adjusting HttpWebRequest Connection Timeout in C#

I believe after lengthy research and searching, I have discovered that what I want to do is probably better served by setting up an asynchronous connection and terminating it after the desired timeout... But I will go ahead and ask anyway! Quick…
JYelton
  • 35,664
  • 27
  • 132
  • 191
56
votes
7 answers

How to know if an HTTP request header value exists

Very simple I'm sure, but driving me up the wall! There is a component that I use in my web application that identifies itself during a web request by adding the header "XYZComponent=true" - the problem I'm having is, how do you check for this in…
Jimbo
  • 22,379
  • 42
  • 117
  • 159
55
votes
7 answers

TLS 1.2 not negotiated in .NET 4.7 without explicit ServicePointManager.SecurityProtocol call

I need to upgrade a .NET application to support a call to an API on a website that only supports TLS 1.2. From what I read, if the application is targeting 4.6 or higher then it will use TLS 1.2 by default. To test I created a Windows Forms app…
Josh
  • 8,219
  • 13
  • 76
  • 123
54
votes
5 answers

Why am I getting "(304) Not Modified" error on some links when using HttpWebRequest?

Any ideas why on some links that I try to access using HttpWebRequest I am getting "The remote server returned an error: (304) Not Modified." in the code? The code I'm using is from Jeff's post here (the page seems to have disappeared, see an…
Greg
  • 34,042
  • 79
  • 253
  • 454