次の方法で共有


WebRequest.Method プロパティ

派生クラスでオーバーライドされると、要求で使用するプロトコル メソッドを取得または設定します。

Public Overridable Property Method As String
[C#]
public virtual string Method {get; set;}
[C++]
public: __property virtual String* get_Method();public: __property virtual void set_Method(String*);
[JScript]
public function get Method() : String;public function set Method(String);

プロパティ値

要求で使用するプロトコル メソッド。

例外

例外の種類 条件
NotSupportedException プロパティが派生クラスでオーバーライドされていないのに、そのプロパティの取得または設定が試行されました。

解説

派生クラスでオーバーライドされると、 Method プロパティは、要求で使用する要求メソッドを格納します。

メモ    WebRequest クラスは、抽象 (Visual Basic では MustInherit) クラスです。実行時の WebRequest インスタンスの実際の動作は、 WebRequest.Create メソッドで返される派生クラスによって決まります。既定値および例外の詳細については、 HttpWebRequestFileWebRequest などの派生クラスの説明を参照してください。

継承時の注意: Method プロパティは、実装されたプロトコル用の任意の有効な要求メソッドを格納できます。既定値は、プロトコル固有なプロパティを設定する必要がない既定の要求/応答トランザクションを提供する値にする必要があります。

使用例

[Visual Basic, C#, C++] 要求がデータをターゲット ホストにポストバックすることを示すために、 Method プロパティを POST に設定する例を次に示します。

 

        ' Create a new request to the mentioned URL.
            Dim myWebRequest As WebRequest = WebRequest.Create("https://www.contoso.com/codesnippets/next.asp")

            ' Create an instance of the RequestState and assign 'myWebRequest' to it's request field.            
            Dim myRequestState As New RequestState()
            myRequestState.request = myWebRequest
            myWebRequest.ContentType = "application/x-www-form-urlencoded"

            ' Set the 'Method' prperty  to 'POST' to post data to a Uri.
            myRequestState.request.Method = "POST"
            myRequestState.request.ContentType = "application/x-www-form-urlencoded"

            ' Start the Asynchronous 'BeginGetRequestStream' method call.    
            Dim r As IAsyncResult = CType(myWebRequest.BeginGetRequestStream(AddressOf ReadCallback, myRequestState), IAsyncResult)

            ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
            Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
            Console.WriteLine(ControlChars.Cr + "The string entered has been successfully posted to the Uri")
            Console.WriteLine("Please wait for the response.......")
            Dim streamResponse As Stream = myWebResponse.GetResponseStream()
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuff(256) As [Char]
            Dim count As Integer = streamRead.Read(readBuff, 0, 256)
            Console.WriteLine(ControlChars.Cr + "The contents of the HTML page are ")
            While count > 0
                Dim outputData As New [String](readBuff, 0, count)
                Console.WriteLine(outputData)
                count = streamRead.Read(readBuff, 0, 256)
            End While

           ' Close the Stream Object.
        streamResponse.Close()
        streamRead.Close()
        allDone.WaitOne()    

        ' Release the HttpWebResponse Resource.
         myWebResponse.Close()    
        Catch e As WebException
            Console.WriteLine(ControlChars.Cr + "WebException Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "Exception Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        End Try
    End Sub ' Main
     
    Private Shared Sub ReadCallback(asynchronousResult As IAsyncResult)
        Try

            ' State of request is set to asynchronous.
            Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
            Dim myWebRequest2 As WebRequest = myRequestState.request

            ' End of the Asynchronus request.
            Dim streamResponse As Stream = myWebRequest2.EndGetRequestStream(asynchronousResult)

            ' Create a string that is to be posted to the uri.
            Console.WriteLine(ControlChars.Cr + "Please enter a string to be posted to (https://www.contoso.com/codesnippets/next.asp) Uri:")
            Dim inputData As String = Console.ReadLine()
            Dim postData As String = "firstone" + ChrW(61) + inputData
            Dim encoder As New ASCIIEncoding()

            ' Convert  the string into a byte array.
            Dim ByteArray As Byte() = encoder.GetBytes(postData)

            ' Write data to the stream.
            streamResponse.Write(ByteArray, 0, postData.Length)
            streamResponse.Close()
            allDone.Set()

        Catch e As WebException
            Console.WriteLine(ControlChars.Cr + "WebException Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "Exception Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        End Try

    End Sub ' ReadCallback 

[C#] 
// Create a new request to the mentioned URL.    
WebRequest myWebRequest= WebRequest.Create("https://www.contoso.com");

// Create an instance of the RequestState and assign 'myWebRequest' to it's request field.    
RequestState myRequestState = new RequestState();
myRequestState.request = myWebRequest;            
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'Method' prperty  to 'POST' to post data to a Uri.
myRequestState.request.Method="POST";
myRequestState.request.ContentType="application/x-www-form-urlencoded";

// Start the Asynchronous 'BeginGetRequestStream' method call.    
IAsyncResult r=(IAsyncResult) myWebRequest.BeginGetRequestStream(new AsyncCallback(ReadCallback),myRequestState);            

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();
Console.WriteLine("\nThe string entered has been successfully posted to the Uri");    
Console.WriteLine("Please wait for the response.......");

Stream streamResponse=myWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe contents of the HTML page are ");    

while (count > 0) 
{
    String outputData = new String(readBuff, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuff, 0, 256);
}

// Close the Stream Object.
streamResponse.Close();
streamRead.Close();
allDone.WaitOne();    

// Release the HttpWebResponse Resource.
myWebResponse.Close();            

        }
        catch(Exception e)
        {
Console.WriteLine(e.ToString());
        }

    }
    private static void ReadCallback(IAsyncResult asynchronousResult)
    {    
        try
        {

// State of request is set to asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
WebRequest  myWebRequest2=myRequestState.request;

// End of the Asynchronus request.
Stream streamResponse=myWebRequest2.EndGetRequestStream(asynchronousResult);

// Create a string that is to be posted to the uri.
Console.WriteLine("\nPlease enter a string to be posted to (https://www.contoso.com) Uri:");
string inputData=Console.ReadLine();
string postData="firstone="+inputData;
ASCIIEncoding encoder = new ASCIIEncoding();

// Convert  the string into a byte array.
byte[] ByteArray = encoder.GetBytes(postData);

// Write data to the stream.
streamResponse.Write(ByteArray,0,postData.Length);
streamResponse.Close();        
allDone.Set();
        }
        catch(Exception e)
        {
Console.WriteLine(e.ToString());
        }
    }

[C++] 
// Create a new request to the mentioned URL.
WebRequest* myWebRequest= WebRequest::Create(S"https://www.contoso.com");

// Create an instance of the RequestState and assign 'myWebRequest' to its request field.
RequestState* myRequestState = new RequestState();
myRequestState->request = myWebRequest;
myWebRequest->ContentType=S"application/x-www-form-urlencoded";

// Set the 'Method' prperty  to 'POST' to post data to a Uri.
myRequestState->request->Method=S"POST";
myRequestState->request->ContentType=S"application/x-www-form-urlencoded";

// Start the Asynchronous 'BeginGetRequestStream' method call.
IAsyncResult* r = 
   dynamic_cast<IAsyncResult*> (myWebRequest->BeginGetRequestStream(new AsyncCallback(r, 
   WebRequest_BeginGetRequeststream::ReadCallback), myRequestState));

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse* myWebResponse = myWebRequest->GetResponse();
Console::WriteLine(S"\nThe String* entered has been successfully posted to the Uri");
Console::WriteLine(S"Please wait for the response.......");

Stream* streamResponse = myWebResponse->GetResponseStream();
StreamReader* streamRead = new StreamReader(streamResponse);
Char readBuff[] = new Char[256];
int count = streamRead->Read(readBuff, 0, 256);
Console::WriteLine(S"\nThe contents of the HTML page are ");

while (count > 0) {
   String* outputData = new String(readBuff, 0, count);
   Console::Write(outputData);
   count = streamRead->Read(readBuff, 0, 256);
}

// Close the Stream Object.
streamResponse->Close();
streamRead->Close();
WebRequest_BeginGetRequeststream::allDone->WaitOne();

// Release the HttpWebResponse Resource.
myWebResponse->Close();

   } catch (Exception* e) {
Console::WriteLine(e);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

WebRequest クラス | WebRequest メンバ | System.Net 名前空間 | HttpWebRequest.Method