WebRequest.Create WebRequest.Create WebRequest.Create WebRequest.Create Method

定义

初始化新的 WebRequestInitializes a new WebRequest.

重载

Create(String) Create(String) Create(String) Create(String)

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

Create(Uri) Create(Uri) Create(Uri) Create(Uri)

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

Create(String) Create(String) Create(String) 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 String String 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()

注解

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

例如,当 URI 以http://https:// requestUri开头时, HttpWebRequest将返回CreateFor 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子代来处理其他请求。 RegisterPrefixCustom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.

方法使用参数创建它传递给新WebRequest的实例。Uri requestUriString CreateThe 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) Create(Uri) Create(Uri) 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 Uri 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.

调用方没有连接到请求的 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()

注解

方法返回在运行时确定为WebRequest的最接近注册匹配项requestUri的类的后代。 CreateThe 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/请求,并创建另一个名为http://www.contoso.com/code/Handler2 的请求来处理请求,则Create可以使用方法返回与指定的 URI。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.

例如,当 URI 以http://https:// requestUri开头时, HttpWebRequest将返回CreateFor 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子代来处理其他请求。 RegisterPrefixCustom 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.

适用于