HttpWebResponse.Method 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得用來傳回回應的方法。
public:
virtual property System::String ^ Method { System::String ^ get(); };
public:
property System::String ^ Method { System::String ^ get(); };
public virtual string Method { get; }
public string Method { get; }
member this.Method : string
Public Overridable ReadOnly Property Method As String
Public ReadOnly Property Method As String
屬性值
字串,含有用來傳回回應的 HTTP 方法。
例外狀況
目前的執行個體已經過處置。
範例
下列範例會檢查 中包含的 Method字串,以判斷 Web 伺服器叫用的 Http 方法。
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( url ) );
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
String^ method;
method = myHttpWebResponse->Method;
if ( String::Compare( method, "GET" ) == 0 )
{
Console::WriteLine( "\nThe 'GET' method was successfully invoked on the following Web Server : {0}",
myHttpWebResponse->Server );
}
// Releases the resources of the response.
myHttpWebResponse->Close();
}
catch ( WebException^ e )
{
Console::WriteLine( "\nWebException raised. The following error occurred : {0}", e->Status );
}
catch ( Exception^ e )
{
Console::WriteLine( "\nThe following Exception was raised : {0}", e->Message );
}
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string method ;
method = myHttpWebResponse.Method;
if (String.Compare(method,"GET") == 0)
Console.WriteLine("\nThe 'GET' method was successfully invoked on the following Web Server : {0}",
myHttpWebResponse.Server);
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
Console.WriteLine("\nWebException raised. The following error occurred : {0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
Try
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Dim method As String
method = myHttpWebResponse.Method
If [String].Compare(method, "GET") = 0 Then
Console.WriteLine(ControlChars.NewLine + "The GET method was successfully invoked on the following Web Server : {0}", myHttpWebResponse.Server)
End If
' Releases the resources of the response.
myHttpWebResponse.Close()
Catch e As WebException
Console.WriteLine(ControlChars.NewLine + "Exception Raised. The following error occurred : {0}", e.Status)
Catch e As Exception
Console.WriteLine(ControlChars.NewLine + "The following exception was raised : {0}", e.Message)
End Try
備註
Method 會傳回用來傳回回應的方法。 常見的 HTTP 方法包括 GET、HEAD、POST、PUT 和 DELETE。