共用方式為


HttpWebResponse.Method Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the method that is used to return the response.

Namespace:  System.Net
Assembly:  System.Net (in System.Net.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property Method As String
public virtual string Method { get; }

Property Value

Type: System.String
A string that contains the HTTP method that is used to return the response.

Exceptions

Exception Condition
ObjectDisposedException

The current instance has been disposed.

Remarks

Method returns the method that is used to return the response. Common HTTP methods are GET, HEAD, POST, PUT, and DELETE. However, only GET and POST methods are currently supported.

Examples


      // HttpWebRequest and HttpWebResponse are members of the 
      // RequestState class defined for this sample


      // Need to subtitute with a URI that works
        Uri url = new Uri("https://www.contoso.com");

        try 
            {   
            // Creates an HttpWebRequest for the specified URL. 
                    HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(url); 

            // Create an instance of the RequestState and assign the previous myHttpWebRequest1
            // object to it's request field.  
            RequestState myRequestState = new RequestState();  
            myRequestState.request = myHttpWebRequest1;

            // Start the asynchronous request.
            IAsyncResult result=
              (IAsyncResult) myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

            allDone.WaitOne();

            outputBlock.Text += "The following method was used: ";
            outputBlock.Text += myRequestState.response.Method;
            outputBlock.Text += "\n";     

            // Release the HttpWebResponse resource.
            myRequestState.response.Close();
        }
            catch(WebException e) 
            {
                outputBlock.Text += "\nException raised!\n";
            outputBlock.Text += "Message: ";
            outputBlock.Text += e.Message;
            outputBlock.Text += "\nStatus: ";
            outputBlock.Text += e.Status;
            outputBlock.Text += "\n";
        }
            catch(Exception e)
              {
            outputBlock.Text += "\nException raised!\n";
            outputBlock.Text += "\nMessage: ";
            outputBlock.Text += e.Message;
            outputBlock.Text += "\n";
              }
      }
    
      private static void RespCallback(IAsyncResult asynchronousResult)
    {  
        try
        {
            // State of request is asynchronous.
            RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
            HttpWebRequest myHttpWebRequest2=myRequestState.request;
            myRequestState.response = (HttpWebResponse) myHttpWebRequest2.EndGetResponse(asynchronousResult);
        }
        catch(WebException e)
            {
            // need to handle exception here
        }
    }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.