是否可以從.NET 2.0客戶端調用Web Api方法?
當然,這是可能的。你可以從任何HTTP兼容的客戶端調用它。客戶端可能不是.NET。
例如.NET 2.0中,你可以使用WebClient
類:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeaders.Accept] = "application/json";
string result = client.DownloadString("http://example.com/values");
// now use a JSON parser to parse the resulting string back to some CLR object
}
,如果你想發佈一些值:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Accept] = "application/json";
var data = Encoding.UTF8.GetBytes("{\"foo\":\"bar\"}");
byte[] result = client.UploadData("http://example.com/values", "POST", data);
// now use a JSON parser to parse the resulting string back to some CLR object
}
肯定的,但最終我們需要轉換成字節再次響應字符串以顯示正確的響應 –