![](/img/trans.png)
[英]Calling methods on an old Web Service build in .Net 2.0 from a WCF Client
[英]Calling Web Api service from a .NET 2.0 client
是否可以從.NET 2.0客戶端調用Web Api方法?
在此參考指南: http : //www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
客戶端的某些dll似乎與.NET 2.0不兼容
有什么方法可以在不添加任何dll的情況下從.NET 2.0客戶端調用Web Api方法?
是否可以從.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);
string resultContent = Encoding.UTF8.GetString(result, 0, result.Length);
//現在使用JSON解析器將結果字符串解析回一些CLR對象}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.