はい、PostAsync
メソッドを使用します。
このメソッドは、インスタンスだけでなく、( WebClientクラスのメソッドUri
と同様に)または文字列のいずれかを取ります。UploadString
HttpContent
インスタンスは、HttpContent
さまざまなタイプのコンテンツに依存しないように設計されており、コンテンツを提供するメカニズム(バイトByteArrayContent
の配列、StreamContentStream
など)だけでなく、構造(MultipartFormDataContent)も指定できます。
そうは言っても、次のように文字列を送信するStringContent
クラスもあります。
// コンテンツ。string post="投稿するコンテンツ";
// The client.
using (client = new HttpClient());
{
// Post.
// Let's assume you're in an async method.
HttpResponseMessage response = await client.Post(
"http://yourdomain/post", new StringContent(post));
// Do something with the response.
}
を指定する必要がある場合はEncoding
、を受け取るコンストラクターがEncoding
あります。これは次のように使用します。
// The client.
using (client = new HttpClient());
{
// Post.
// Let's assume you're in an async method.
HttpResponseMessage response = await client.Post(
"http://yourdomain/post", new StringContent(post),
Encoding.ASCII);
// Do something with the response.
}
そこからHttpResponseMessage
、応答が送信されるときに処理することが問題になります(それが重要な場合、一方向の操作でない場合)。