Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
HttpWebRequest Class

Provides an HTTP-specific implementation of the WebRequest class.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
Public Class HttpWebRequest _
    Inherits WebRequest _
    Implements ISerializable
Visual Basic (Usage)
Dim instance As HttpWebRequest
C#
[SerializableAttribute]
public class HttpWebRequest : WebRequest, 
    ISerializable
Visual C++
[SerializableAttribute]
public ref class HttpWebRequest : public WebRequest, 
    ISerializable
JScript
public class HttpWebRequest extends WebRequest implements ISerializable

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.

The following table lists the HTTP headers that are set either by properties or methods or the system.

Header

Set by

Accept

Set by the Accept property.

Connection

Set by the Connection property, KeepAlive property.

Content-Length

Set by the ContentLength property.

Content-Type

Set by the ContentType property.

Expect

Set by the Expect property.

Date

Set by the system to current date.

Host

Set by the system to current host information.

If-Modified-Since

Set by the IfModifiedSince property.

Range

Set by the AddRange method.

Referer

Set by the Referer property.

Transfer-Encoding

Set by the TransferEncoding property (the SendChunked property must be true).

User-Agent

Set by the UserAgent property.

NoteNote:

HttpWebRequest is registered automatically. You do not need to call the RegisterPrefix method to register System.Net..::.HttpWebRequest before using URIs beginning with http:// or https://.

The local computer or application config file may specify that a default proxy be used. If the Proxy property is specified, then the proxy settings from the Proxy property override the local computer or application config file and the HttpWebRequest instance will use the proxy settings specified. If no proxy is specified in a config file and the Proxy property is unspecified, the HttpWebRequest class uses the proxy settings inherited from Internet Explorer on the local computer. If there are no proxy settings in Internet Explorer, the request is sent directly to the server.

The HttpWebRequest class parses a proxy bypass list with wildcard characters inherited from Internet Explorer differently than the bypass list is parsed directly by Internet Explorer. For example, the HttpWebRequest class will parse a bypass list of "nt*" from Internet Explorer as a regular expression of "nt.$". This differs from the native behavior of Internet Explorer. So a URL of "http://intxxxxx" would bypass the proxy using the HttpWebRequest class, but would not bypass the proxy using Internet Explorer.

NoteNote:

The Framework caches SSL sessions as they are created and attempts to reuse a cached session for a new request, if possible. When attempting to reuse an SSL session, the Framework uses the first element of ClientCertificates (if there is one), or tries to reuse an anonymous sessions if ClientCertificates is empty.

NoteNote:

For security reasons, cookies are disabled by default. If you want to use cookies, use the CookieContainer property to enable cookies.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: An HTTP POST request over Secure Sockets Layer (SSL) will fail with a SocketException if the request goes through a proxy and all of the following conditions are also met:

  • Preauthentication is disabled on the server.

  • The server requires Basic, Digest, Negotiate, or Windows NT Challenge/Response (NTLM) authentication.

  • KeepAlive is set to false.

  • X.509 client authentication is not used.

The following code example creates an HttpWebRequest for the URI http://www.contoso.com/.

Visual Basic
Dim myReq As HttpWebRequest = _
    WebRequest.Create("http://www.contoso.com/")

C#
HttpWebRequest myReq =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com/");

Visual C++
HttpWebRequest^ myReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.contoso.com/" ));
JScript
var myReq : HttpWebRequest = HttpWebRequest(WebRequest.Create("http://www.contoso.com/"))

CPP_OLD
HttpWebRequest* myReq =
dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com/"));

  • WebPermission 

    to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect

System..::.Object
  System..::.MarshalByRefObject
    System.Net..::.WebRequest
      System.Net..::.HttpWebRequest
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
webpage risticate      Mateen Deshmukh ... Noelle Mallory - MSFT   |   Edit   |   Show History

Hi
I m doing a project internet protected.
in project i want risticate website in system webBorwser
Like if i enter www.google.com in addresbare then web Browse not open this site
so how i can do this plz give me some idea

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Tags What's this?: Add a tag
Flag as ContentBug
Example in C#      kodekraft ... Tek4.com   |   Edit   |   Show History

Example usage:

private string StringGetWebPage(String uri)
{
const int bufSizeMax = 65536; // max read buffer size conserves memory
const int bufSizeMin = 8192; // min size prevents numerous small reads
StringBuilder sb;




// A WebException is thrown if HTTP request fails
try
{

// Create an HttpWebRequest using WebRequest.Create (see .NET docs)!
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);




// Execute the request and obtain the response stream
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();



// Content-Length header is not trustable, but makes a good hint.
// Responses longer than int size will throw an exception here!
int length = (int)response.ContentLength;



// Use Content-Length if between bufSizeMax and bufSizeMin
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;


// Allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
sb = new StringBuilder(bufSize);






// Read response stream until end
while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
sb.Append(Encoding.UTF8.GetString(buf, 0, length));

}
catch (Exception ex)
{
sb = new StringBuilder(ex.Message);
}
return sb.ToString();
}
Flag as ContentBug
See also      Asaf Shelly   |   Edit   |   Show History

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx


request.Credentials = new NetworkCredential("user_name", "password");

Tags What's this?: Add a tag
Flag as ContentBug
Sending POST date over a proxy      Alien333 ... CULE_Developer   |   Edit   |   Show History

hi

i'm trying to write an application wich uses wabpages to get cetain information
but to acces these pages i need to send POST data i did that by sending it with the .GetRequestStream() function
afterwards i also wanted to to this and use a proxy and a added a working proxy with .Proxy statment
but now it seems it doesnt work anymore i get the webpage as if the post date wasnt send
so i assumed the doesnt get send
can anyone help me with this

Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Exception thrown WebRequest.Create      bas vijfwinkel   |   Edit   |   Show History
If you get an exception thrown by WebRequest.Create, check if you are using threads.

If the main program that creates the thread ends, the thread might still be able to continue running

but it will cause all kinds of weird exceptions in other libraries like WebRequest.

Therefore, make sure that the main program (or UnitTest) does not terminate before the thread has finished.


Careful when sleeping on the Compact Framework      Richard Arthur   |   Edit   |   Show History
If you are building a Compact Framework application, and you create different instances of HttpWebRequest on different threads, you might run into problems. In particular, if the top of the stack has a variable of type HttpWebRequest and you call Thread.Sleep(), then other threads may not be able to use instances of HttpWebRequest. Make sure to do the Thread.Sleep() in the calling method, after that variable has left scope so that it can be garbage collected. This will allow for multiple threads to create these object instances.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker