System.Net.WebRequest.Create Method

Initializes a new System.Net.WebRequest instance for the specified URI scheme.

Syntax

public static WebRequest Create (Uri requestUri)

Parameters

requestUri
A Uri containing the URI of the requested resource.

Returns

A System.Net.WebRequest descendant for the specified URI scheme.

Exceptions

TypeReason
ArgumentNullException requestUri is null.
NotSupportedExceptionThe request scheme specified in requestUri is not registered.
System.Security.SecurityExceptionThe caller does not have permission to connect to the requested URI or a URI that the request is redirected to.

Remarks

The WebRequest.Create(Uri) method returns a descendant of the System.Net.WebRequest class determined at run time as the closest registered match for requestUri.

For example, if you create a System.Net.WebRequest descendant, Handler1, to handle requests to http://www.contoso.com/text/ and another named Handler2 to handle requests to http://www.contoso.com/code/, you can use WebRequest.Create(Uri) method to return the WebRequest descendant associated with either specified URI.

To return a descendant of the System.Net.WebRequest class based on only the scheme portion of a URI, use the WebRequest.CreateDefault(Uri) method.

For example, when a URI beginning with http:// or https:// is passed in requestUri, an System.Net.HttpWebRequest is returned by WebRequest.Create(Uri). If a URI beginning with ftp:// is passed instead, the WebRequest.Create(Uri) method will return a System.Net.FileWebRequest instance. If a URI beginning with file:// is passed instead, the WebRequest.Create(Uri) method will return a System.Net.FileWebRequest instance.

The pre-registered reserve types already registered include the following:

  • http://

  • https://

  • ftp://

  • file://

The .NET Framework includes support for the http://, https://, ftp://, and file:// URI schemes. Custom System.Net.WebRequest descendants to handle other requests are registered with the WebRequest.RegisterPrefix(string, IWebRequestCreate) method.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Permissions

TypeReason
!:System.Security.Permissions.WebPermissionRequires permission to connect to the requested URI. See NetworkAccess.Connect.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0