私はある種の RESTful に似た API を構築しようとしていますが、最初のドラフトはおそらく実際の RESTful 設計パターンに近いものではないことを認識しています。ただし、私の本当の質問は、JSON を使用してサービスをどのように使用すればよいですか?
私のいわゆる実世界の例では、ユーザーにサービス経由でサインインしてもらいたいので、この AuthenticationController を持っています
namespace RESTfulService.Controllers
{
public class AuthenticationController : ApiController
{
public string Get(string username, string password)
{
// return JSON-object or JSON-status message
return "";
}
public string Get()
{
return "";
}
}
}
このテクノロジーの人気が高まっていることを考えると、サービスを利用するために必要なコードはごくわずかだと思いました。json.net のようなサードパーティのパッケージを使用して、JSON を手動でシリアル化する必要がありますか? 以下は、クライアント向けの私のドラフトです
private static bool DoAuthentication(string username, string password)
{
var client = InitializeHttpClient();
HttpResponseMessage response = client.GetAsync("/api/rest/authentication").Result;
if (response.IsSuccessStatusCode)
{
//retrieve JSON-object or JSON-status message
}
else
{
// Error
}
return true;
}
private static HttpClient InitializeHttpClient()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
サービスから JSON を送信する方法と、クライアントで JSON を解釈する方法を教えてください。