WebRequest.Create Method

Definition

Initializes a new WebRequest.

Overloads

Create(String)
Obsolete.

Initializes a new WebRequest instance for the specified URI scheme.

Create(Uri)
Obsolete.

Initializes a new WebRequest instance for the specified URI scheme.

Create(String)

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.

Initializes a new WebRequest instance for the specified URI scheme.

public:
 static System::Net::WebRequest ^ Create(System::String ^ requestUriString);
public static System.Net.WebRequest Create (string requestUriString);
[System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Net.WebRequest Create (string requestUriString);
static member Create : string -> System.Net.WebRequest
[<System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Create : string -> System.Net.WebRequest
Public Shared Function Create (requestUriString As String) As WebRequest

Parameters

requestUriString
String

The URI that identifies the Internet resource.

Returns

A WebRequest descendant for the specific URI scheme.

Attributes

Exceptions

The request scheme specified in requestUriString has not been registered.

requestUriString is null.

The caller does not have WebPermissionAttribute permission to connect to the requested URI or a URI that the request is redirected to.

The URI specified in requestUriString is not a valid URI.

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

Examples

The following example uses Create to instantiate an HttpWebRequest instance. A string representing the target URL is used as the constructor parameter.

Uri^ ourUri = gcnew Uri( url );

// Create a 'WebRequest' object with the specified url.
WebRequest^ myWebRequest = WebRequest::Create( url );

// Send the 'WebRequest' and wait for response.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if ( ourUri->Equals( myWebResponse->ResponseUri ) )
{
   Console::WriteLine( "\nRequest Url : {0} was not redirected", url );
}
else
{
   Console::WriteLine( "\nRequest Url : {0} was redirected to {1}", url, myWebResponse->ResponseUri );
}

// Release resources of response object.
myWebResponse->Close();
Uri ourUri = new Uri(url);            

// Create a 'WebRequest' object with the specified url. 
WebRequest myWebRequest = WebRequest.Create(url); 

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if (ourUri.Equals(myWebResponse.ResponseUri))
    Console.WriteLine("\nRequest Url : {0} was not redirected",url);   
else
    Console.WriteLine("\nRequest Url : {0} was redirected to {1}",url,myWebResponse.ResponseUri);   
// Release resources of response object.
myWebResponse.Close();

Dim ourUri As New Uri(url)
' Create a 'WebRequest' object with the specified url. 

Dim myWebRequest As WebRequest = WebRequest.Create(url)

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' "ResponseUri" property is used to get the actual Uri from where the response was attained.
If ourUri.Equals(myWebResponse.ResponseUri) Then
    Console.WriteLine(ControlChars.Cr + "Request Url : {0} was not redirected", url)
Else
    Console.WriteLine(ControlChars.Cr + "Request Url : {0} was redirected to {1}", url, myWebResponse.ResponseUri)
End If 

' Release resources of response object.
myWebResponse.Close()

Remarks

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

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

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

  • http://

  • https://

  • ftp://

  • file://

.NET includes support for the http://, https://, ftp://, and file:// URI schemes. Custom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.

The Create method uses the requestUriString parameter to create a Uri instance that it passes to the new WebRequest.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

See also

Applies to

Create(Uri)

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.

Initializes a new WebRequest instance for the specified URI scheme.

public:
 static System::Net::WebRequest ^ Create(Uri ^ requestUri);
public static System.Net.WebRequest Create (Uri requestUri);
[System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Net.WebRequest Create (Uri requestUri);
static member Create : Uri -> System.Net.WebRequest
[<System.Obsolete("WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.", DiagnosticId="SYSLIB0014", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Create : Uri -> System.Net.WebRequest
Public Shared Function Create (requestUri As Uri) As WebRequest

Parameters

requestUri
Uri

A Uri containing the URI of the requested resource.

Returns

A WebRequest descendant for the specified URI scheme.

Attributes

Exceptions

The request scheme specified in requestUri is not registered.

requestUri is null.

The caller does not have WebPermissionAttribute permission to connect to the requested URI or a URI that the request is redirected to.

Examples

The following example uses Create to instantiate an HttpWebRequest instance. A Uri representing the target URL is used as the constructor parameter.

// Create a new 'Uri' object with the specified string.
Uri^ myUri = gcnew Uri( "http://www.contoso.com" );
// Create a new request to the above mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( myUri );
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();
// Create a new 'Uri' object with the specified string.
Uri myUri =new Uri("http://www.contoso.com");
// Create a new request to the above mentioned URL.	
WebRequest myWebRequest= WebRequest.Create(myUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse= myWebRequest.GetResponse();
' Create a new 'Uri' object with the specified string.
Dim myUri As New Uri("http://www.contoso.com")
' Create a new request to the above mentioned URL.	
Dim myWebRequest As WebRequest = WebRequest.Create(myUri)
'  Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

Remarks

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

For example, if you create a 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 Create method to return the WebRequest descendant associated with either specified URI.

To return a descendant of the WebRequest class based on only the scheme portion of a URI, use the CreateDefault method.

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

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

  • http://

  • https://

  • ftp://

  • file://

.NET includes support for the http://, https://, ftp://, and file:// URI schemes. Custom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

Applies to