Table of Contents

Class HttpWebRequest

Namespace
System.Net
Assembly
System.Net.Http.dll

Provides an HTTP-specific implementation of the WebRequest class.

public class HttpWebRequest : WebRequest, IDisposable
Inheritance
HttpWebRequest
Implements
Inherited Members
Extension Methods

Remarks

This class does the main work of the request: it collects the header information from the user, exposes the Stream for outgoing entity data, and processes the incoming request.

Properties

Accept

Gets or sets the value of the Accept HTTP header.

public string Accept { get; set; }

Property Value

string

The value of the Accept HTTP header. The default value is null.

Address

Gets the URI for this request.

public Uri Address { get; }

Property Value

Uri

A Uri that identifies the Internet resource that actually responds to the request. The default is the URI used by the WebRequest.Create(Uri) method to initialize the request.

Remarks

This value is always the same as the RequestUri property, because automatic re-direction isn't supported.

AllowAutoRedirect

Gets a value that indicates whether the request should follow redirection responses. This value is always false, because Autodirect isn't supported.

public bool AllowAutoRedirect { get; }

Property Value

bool

This value is always false, because Autodirect isn't supported.

AllowWriteStreamBuffering

Gets or sets a value that indicates whether to buffer the data sent to the Internet resource.

public bool AllowWriteStreamBuffering { get; set; }

Property Value

bool

true to enable buffering of the data sent to the Internet resource; false to disable buffering. The default is true.

ContentLength

Gets or sets the Content-Length of the request entity body.

public override long ContentLength { get; set; }

Property Value

long

The number of bytes of data to send to the Internet resource. The default is -1, which indicates the property has not been set and that there is no request data to send.

Remarks

Getting this property returns the last value set, or -1 if no value has been set. Setting it sets the content length, and the application must write that much data to the stream. This property interacts with HttpWebRequest.SendChunked.

ContentType

Gets or sets the type of the entity body (the value of the content type).

public override string ContentType { get; set; }

Property Value

string

The value of the Content-type HTTP header. The default value is null.

Remarks

Setting to null clears the content-type.

ContinueDelegate

Gets or sets the delegate used to signal on Continue callback.

public HttpContinueDelegate ContinueDelegate { get; set; }

Property Value

HttpContinueDelegate

A delegate that implements the callback method that executes when an HTTP Continue response is returned from the Internet resource. The default value is null.

Remarks

This property gets or sets the delegate method called when an HTTP 100-continue response is received from the Internet resource.

Credentials

Set or Get NetworkCredential if user have send user name and password.

public NetworkCredential Credentials { get; set; }

Property Value

NetworkCredential

DefaultMaximumResponseHeadersLength

Gets or sets the default maximum allowed length of the response headers.

public static int DefaultMaximumResponseHeadersLength { get; set; }

Property Value

int

The default maximum allowed length of the response headers.

Remarks

On creation of an HttpWebRequest instance, this value is used for the MaximumResponseHeadersLength property.

Expect

Gets or sets the value of the Expect HTTP header.

public string Expect { get; set; }

Property Value

string

The contents of the Expect HTTP header. The default value is null.

Remarks

When setting this property, null clears the Expect (except for the 100-continue value).

HaveResponse

Gets whether a response has been received from an Internet resource.

public bool HaveResponse { get; }

Property Value

bool

true if a response has been received; otherwise, false.

Headers

A collection of HTTP headers stored as name/value pairs.

public override WebHeaderCollection Headers { get; set; }

Property Value

WebHeaderCollection

A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.

Remarks

The following header values are set through properties on the HttpWebRequest class: Accept, Connection, Content-Length, Content-Type, Expect, Range, Referer, Transfer-Encoding, and User-Agent. Trying to set these header values by using WebHeaderCollection.Add(string, string)() will raise an exception. Date and Host are set internally.

HttpsAuthentCert

Gets or sets the root CA certificate used to authenticate with https servers. This certificate is used only for https connections; http connections do not require this.

public X509Certificate HttpsAuthentCert { get; set; }

Property Value

X509Certificate

IfModifiedSince

Gets the IfModifiedSince value of HttpKnownHeaderNames.

public DateTime IfModifiedSince { get; }

Property Value

DateTime

A DateTime that contains the contents of the If-Modified-Since HTTP header. The default value is the current date and time.

Remarks

The setter for this property isn't supported, because a function that formats the time isn't implemented.

null clears the IfModifiedSince header.

KeepAlive

Gets or sets whether to use a persistent connection, if available.

public bool KeepAlive { get; set; }

Property Value

bool

true if the request to the Internet resource should contain a Connection HTTP header with the value Keep-alive; otherwise, false. The default is true.

MaximumAutomaticRedirections

Gets the maximum number of automatic redirections. This value is always zero, because auto-redirection isn't supported.

public int MaximumAutomaticRedirections { get; }

Property Value

int

This value is always zero, because auto-redirection isn't supported.

MaximumResponseHeadersLength

Gets or sets the maximum allowed length of the response headers.

public int MaximumResponseHeadersLength { get; set; }

Property Value

int

The length, in kilobytes (1024 bytes), of the response headers.

Remarks

The length of the response header includes the response status line and any extra control characters that are received as part of HTTP protocol. A value of -1 means no limit is imposed on the response headers; a value of 0 means that all requests fail. If this property is not explicitly set, it defaults to the value of the DefaultMaximumResponseHeadersLength property.

Method

Gets or sets the HTTP method of this request.

public override string Method { get; set; }

Property Value

string

The request method to use to contact the Internet resource. The default value is GET.

Remarks

This method represents the initial origin verb, which is unchanged and unaffected by redirects.

ProtocolVersion

Gets or sets the HTTP protocol version for this request.

public Version ProtocolVersion { get; set; }

Property Value

Version

The HTTP version to use for the request. The default is Version11.

Proxy

Gets or sets the proxy for the request.

public override IWebProxy Proxy { get; set; }

Property Value

IWebProxy

The IWebProxy object to use to proxy the request. null indicates that no proxy will be used.

ReadWriteTimeout

Gets or sets a timeout in milliseconds when writing to or reading from a stream.

public int ReadWriteTimeout { get; set; }

Property Value

int

The number of milliseconds before the writing or reading times out. The default value is 300,000 milliseconds (5 minutes).

Remarks

This property is used to control the timeout when calling Read(byte[], int, int) and Write(byte[], int, int). This property affects Streams returned from GetResponse().GetResponseStream()() and GetResponse().GetRequestStream()().

Referer

Gets or sets the value of the Referer HTTP header.

public string Referer { get; set; }

Property Value

string

The value of the Referer HTTP header. The default value is null.

Remarks

This header value is misspelled intentionally.

RequestUri

Gets the original Uniform Resource Identifier (URI) of the request.

public override Uri RequestUri { get; }

Property Value

Uri

A Uri that contains the URI of the Internet resource passed to the WebRequest.Create(Uri) method.

Remarks

The URI object was created by the constructor and is always non-null. The URI object will always be the base URI, because automatic re-directs aren't supported.

SendChunked

Gets or sets the state of chunk transfer send mode.

public bool SendChunked { get; set; }

Property Value

bool

true to send data to the Internet resource in segments; otherwise, false. The default value is false.

Remarks

If true, bits are uploaded and written using the Chunked property of HttpWriteMode.

SslProtocols

Gets or sets the TLS/SSL protocol used by the HttpWebRequest class.

public SslProtocols SslProtocols { get; set; }

Property Value

SslProtocols

One of the values defined in the SslProtocols enumeration.

Remarks

Setting this property is mandatory when performing HTTPS requests, otherwise the authentication will fail.

This property is specific to nanoFramework. There is no equivalent in the .NET API.

SslVerification

Gets or sets

public SslVerification SslVerification { get; set; }

Property Value

SslVerification

Timeout

Gets or sets the timeout value in milliseconds for the GetResponse() and GetRequestStream() methods.

public override int Timeout { get; set; }

Property Value

int

The number of milliseconds to wait before the request times out. The default is 100,000 milliseconds (100 seconds).

Remarks

Overrides the Timeout property of WebRequest.

TransferEncoding

Gets or sets the TransferEncoding HTTP header.

public string TransferEncoding { get; set; }

Property Value

string

The value of the Transfer-encoding HTTP header. The default value is null.

Remarks

null clears the transfer encoding except for the Chunked setting.

UserAgent

Gets or sets the value of the User-Agent HTTP header.

public string UserAgent { get; set; }

Property Value

string

The value of the User-agent HTTP header. The default value is null.

Methods

AddRange(int)

Adds a range header to a request for a specific range from the beginning or end of the requested data.

public void AddRange(int range)

Parameters

range int

Start of the range. The end of the range is the end of the existing data.

AddRange(int, int)

Adds a byte range header to the request for a specified range.

public void AddRange(int from, int to)

Parameters

from int

The start of the range.

to int

The end of the range.

AddRange(string, int)

Adds a range header to a request for a specific range from the beginning or end of the requested data.

public void AddRange(string rangeSpecifier, int range)

Parameters

rangeSpecifier string

The description of the range, such as "bytes".

range int

The range value.

AddRange(string, int, int)

Adds a range header to a request for a specified range.

public void AddRange(string rangeSpecifier, int from, int to)

Parameters

rangeSpecifier string

The description of the range, such as "bytes".

from int

The start of the range.

to int

The end of the range.

Remarks

rangeSpecifier would normally be specified as "bytes", since this is the only range specifier recognized by most HTTP servers. Setting rangeSpecifier to some other string allows support for custom range specifiers other than bytes. The byte-range specifier is defined in RFC 2616 by the IETF.

Dispose(bool)

Closes a response stream, if present.

protected override void Dispose(bool disposing)

Parameters

disposing bool

Not used.

GetRequestStream()

Submits a request with HTTP headers to the server, and returns a Stream object to use to write request data.

public override Stream GetRequestStream()

Returns

Stream

A Stream to use to write request data.

Remarks

Used for POST of PUT requests.

GetResponse()

Returns a response from an Internet resource. Overrides the WebRequest.GetResponse() method.

public override WebResponse GetResponse()

Returns

WebResponse

The response from the Internet resource.

Reset()

Reset

public void Reset()