简体   繁体   English

从.NET 2.0客户端调用Web Api服务

[英]Calling Web Api service from a .NET 2.0 client

Is it possible to call a Web Api method from a .NET 2.0 client? 是否可以从.NET 2.0客户端调用Web Api方法?

Referring to the guide here: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client 在此参考指南: http : //www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

Some of these dlls for the client doesn't seem to be compatible with .NET 2.0 客户端的某些dll似乎与.NET 2.0不兼容

Are there any ways to call a Web Api method from a .NET 2.0 client without adding any dlls? 有什么方法可以在不添加任何dll的情况下从.NET 2.0客户端调用Web Api方法?

Is it possible to call a Web Api method from a .NET 2.0 client? 是否可以从.NET 2.0客户端调用Web Api方法?

Of course that it's possible. 当然有可能。 You can call it from absolutely any HTTP compliant client. 您可以从任何符合HTTP标准的客户端中调用它。 The client might not even be .NET. 客户端甚至可能不是.NET。

For example in .NET 2.0 you could use the WebClient class: 例如,在.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
}

and if you wanted to POST some value: 如果您想发布一些值:

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);        

// now use a JSON parser to parse the resulting string back to some CLR object } //现在使用JSON解析器将结果字符串解析回一些CLR对象}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM