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

識別網際網路資源的 URI。The URI that identifies the Internet resource.

傳回

WebRequest

特定 URI 配置的 WebRequest 子代。A WebRequest descendant for the specific URI scheme.

例外狀況

requestUriString 中指定的要求配置尚未註冊。The request scheme specified in requestUriString has not been registered.

requestUriStringnullrequestUriString is null.

呼叫端沒有 WebPermissionAttribute 權限以連線至要求的 URI,或連線至要求重新導向目標的 URI。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 WebRequest 執行時間所決定之類別的下階,做為最接近的註冊相符項 requestUriThe Create method returns a descendant of the WebRequest class determined at run time as the closest registered match for requestUri.

例如,當 http:// 傳入或的 URI 傳入時 https:// requestUriHttpWebRequest 會傳回 CreateFor example, when a URI beginning with http:// or https:// is passed in requestUri, an HttpWebRequest is returned by Create. 如果以傳遞的 URI ftp:// 改為傳遞,則 Create 方法會傳回 FtpWebRequest 實例。If a URI beginning with ftp:// is passed instead, the Create method will return a FtpWebRequest instance. 如果以傳遞的 URI file:// 改為傳遞,則 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:// 和 URI 配置的支援 file://.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.

Create方法 requestUriString 會使用參數來建立 Uri 它傳遞給新的實例 WebRequestThe 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,包含要求資源的 URI。A Uri containing the URI of the requested resource.

傳回

WebRequest

WebRequest 子代,屬於指定的 URI 配置。A WebRequest descendant for the specified URI scheme.

例外狀況

requestUri 中指定的要求配置未登錄。The request scheme specified in requestUri is not registered.

requestUrinullrequestUri is null.

呼叫端沒有 WebPermissionAttribute 權限以連線至要求的 URI,或連線至要求重新導向目標的 URI。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 WebRequest 執行時間所決定之類別的下階,做為最接近的註冊相符項 requestUriThe 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.

WebRequest 只要根據 URI 的配置部分傳回類別的子代,請使用 CreateDefault 方法。To return a descendant of the WebRequest class based on only the scheme portion of a URI, use the CreateDefault method.

例如,當 http:// 傳入或的 URI 傳入時 https:// requestUriHttpWebRequest 會傳回 CreateFor example, when a URI beginning with http:// or https:// is passed in requestUri, an HttpWebRequest is returned by Create. 如果以傳遞的 URI ftp:// 改為傳遞,則 Create 方法會傳回 FileWebRequest 實例。If a URI beginning with ftp:// is passed instead, the Create method will return a FileWebRequest instance. 如果以傳遞的 URI file:// 改為傳遞,則 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:// 和 URI 配置的支援 file://.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.

適用於