0
私はこのソリューションを使用します。エンドポイントにファイルを送る方法RESTful C#?サーバーにデータを送信するために
https://stackoverflow.com/a/19664927/8033752
しかし、どのようにPOSTによるRESTfulなconcretエンドポイントにデータを送信するためには?
私はこのソリューションを使用します。エンドポイントにファイルを送る方法RESTful C#?サーバーにデータを送信するために
https://stackoverflow.com/a/19664927/8033752
しかし、どのようにPOSTによるRESTfulなconcretエンドポイントにデータを送信するためには?
これは私がそれを行う方法です - PATCH
の場合も同様です。以下のコードは、原則を示すためにコメントと例外処理が制限されているように最適化されています。
/// <summary>
/// Calls a JSON/REST POST service.
/// </summary>
/// <typeparam name="TValue">Type of to be sent value.</typeparam>
/// <param name="loadPackage">Mandatory. The package the post call shall carry.</param>
/// <param name="requestUri">Mandatory. URI which shall be called.</param>
/// <returns>Returns the plain service response.</returns>
public async Task<HttpResponseMessage> CallPostServicePlainAsync<TValue>(
TValue loadPackage,
string requestUri)
{
using (var httpClient = CreateHttpClient()) // or just `new HttpClient()` plus magic
{
bool isStream = typeof(TValue) == typeof(Stream);
bool isMultipleStreams = typeof(TValue) == typeof(Stream[]);
if (isStream || isMultipleStreams)
{
var message = new HttpRequestMessage();
message.Method = HttpMethod.Post; // that's what makes it a POST :-)
var content = new MultipartFormDataContent();
if (isStream) // single stream
content.Add(new StreamContent(loadPackage as Stream));
else if (isMultipleStreams) // this is an array of streams
foreach (Stream stream in loadPackage as Stream[])
content.Add(new StreamContent(stream));
else // oops
throw new NotImplementedException("incorrect load package.");
message.Content = content;
message.RequestUri = new Uri(requestUri);
return await httpClient.SendAsync(message).ConfigureAwait(false);
} else {
// standard serializable content (not streams)
...
}
}
}
あなたは「安らか」とはどういう意味ですか:それは、単一および複数のファイルストリームを含む任意のシリアライズ可能なコンテンツを受け入れることができるように
の例では、汎用的な非同期メソッドのですか? REST APIは単なるHTTPエンドポイントであり、REST部分は基礎となるHTTP要求のセマンティクスを変更しません。 – CodeCaster