たとえば、Web API があります: http://example.com/api/product。
この Web API を使用する C# クライアントがあります。製品の全リストを取得するようなもの。
// List all products.
HttpResponseMessage response = client.GetAsync("api/products").Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var products = response.Content.ReadAsAsync<IEnumerable<Product>>().Result;
foreach (var p in products)
{
Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Price, p.Category);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
C# クライアントからサーバーの API にユーザー名とパスワードを渡すにはどうすればよいですか? 私が欲しいのは、C# クライアントが Web API から製品リスト全体を取得するときです。
クライアントは、ユーザー名とパスワードをサーバーの API に送信します。サーバーの Web API がデータベースから承認されたユーザーであるかどうかを確認する場合、そうでない場合は製品リストを取得しません。