WebRequest.Create 方法

定义

初始化新的 WebRequestInitializes a new WebRequest.

重载

Create(String)

为指定的 URI 方案初始化新的 WebRequest 实例。Initializes a new WebRequest instance for the specified URI scheme.

Create(Uri)

为指定的 URI 方案初始化新的 WebRequest 实例。Initializes a new WebRequest instance for the specified URI scheme.

Create(String)

为指定的 URI 方案初始化新的 WebRequest 实例。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);
static member Create : string -> System.Net.WebRequest
Public Shared Function Create (requestUriString As String) As WebRequest

参数

requestUriString
String

标识 Internet 资源的 URI。The URI that identifies the Internet resource.

返回

特定 URI 方案的 WebRequest 子代。A WebRequest descendant for the specific URI scheme.

异常

requestUriString 中指定的请求方案尚未注册。The request scheme specified in requestUriString has not been registered.

requestUriStringnullrequestUriString is null.

调用方没有连接到请求的 URI 或请求重定向到的 URI 的 WebPermissionAttribute 权限。The caller does not have WebPermissionAttribute permission to connect to the requested URI or a URI that the request is redirected to.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

requestUriString 中指定的 URI 不是有效的 URI。The URI specified in requestUriString is not a valid URI.

示例

下面的示例使用 Create 来实例化 HttpWebRequest 实例。The following example uses Create to instantiate an HttpWebRequest instance. 表示目标 URL 的字符串用作构造函数参数。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()

注解

Create 方法返回在运行时确定为 requestUri最近注册的匹配项的 WebRequest 类的后代。The Create method returns a descendant of the WebRequest class determined at run time as the closest registered match for requestUri.

例如,在 requestUri中传递以 http://https:// 开头的 URI 时,Create返回 HttpWebRequestFor example, when a URI beginning with http:// or https:// is passed in requestUri, an HttpWebRequest is returned by Create. 如果改为传递以 ftp:// 开头的 URI,则 Create 方法将返回 FtpWebRequest 的实例。If a URI beginning with ftp:// is passed instead, the Create method will return a FtpWebRequest instance. 如果改为传递以 file:// 开头的 URI,则 Create 方法将返回 FileWebRequest 的实例。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 包括对 http://https://ftp://file:// URI 方案的支持。.NET includes support for the http://, https://, ftp://, and file:// URI schemes. 用于处理其他请求的自定义 WebRequest 子代将注册到 RegisterPrefix 方法。Custom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.

Create 方法使用 requestUriString 参数创建它传递到新 WebRequestUri 实例。The Create method uses the requestUriString parameter to create a Uri instance that it passes to the new WebRequest.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

Create(Uri)

为指定的 URI 方案初始化新的 WebRequest 实例。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);
static member Create : Uri -> System.Net.WebRequest
Public Shared Function Create (requestUri As Uri) As WebRequest

参数

requestUri
Uri

包含请求的资源的 URI 的 UriA Uri containing the URI of the requested resource.

返回

指定的 URI 方案的 WebRequest 子代。A WebRequest descendant for the specified URI scheme.

异常

requestUri 中指定的请求方案未注册。The request scheme specified in requestUri is not registered.

requestUrinullrequestUri is null.

调用方没有连接到请求的 URI 或请求重定向到的 URI 的 WebPermissionAttribute 权限。The caller does not have WebPermissionAttribute permission to connect to the requested URI or a URI that the request is redirected to.

示例

下面的示例使用 Create 来实例化 HttpWebRequest 实例。The following example uses Create to instantiate an HttpWebRequest instance. 表示目标 URL 的 Uri 用作构造函数参数。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()

注解

Create 方法返回在运行时确定为 requestUri最近注册的匹配项的 WebRequest 类的后代。The Create method returns a descendant of the WebRequest class determined at run time as the closest registered match for requestUri.

例如,如果创建一个 WebRequest 子代 Handler1 来处理对 http://www.contoso.com/text/ 的请求,并创建另一个名为 Handler2 的请求来处理请求 http://www.contoso.com/code/,则可以使用 Create 方法返回与任一指定 URI 关联的 WebRequest 子代。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.

若要仅基于 URI 的方案部分返回 WebRequest 类的子代,请使用 CreateDefault 方法。To return a descendant of the WebRequest class based on only the scheme portion of a URI, use the CreateDefault method.

例如,在 requestUri中传递以 http://https:// 开头的 URI 时,Create返回 HttpWebRequestFor example, when a URI beginning with http:// or https:// is passed in requestUri, an HttpWebRequest is returned by Create. 如果改为传递以 ftp:// 开头的 URI,则 Create 方法将返回 FileWebRequest 的实例。If a URI beginning with ftp:// is passed instead, the Create method will return a FileWebRequest instance. 如果改为传递以 file:// 开头的 URI,则 Create 方法将返回 FileWebRequest 的实例。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 包括对 http://https://ftp://file:// URI 方案的支持。.NET includes support for the http://, https://, ftp://, and file:// URI schemes. 用于处理其他请求的自定义 WebRequest 子代将注册到 RegisterPrefix 方法。Custom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于