gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:03:30 26 4
gpt4 key购买 nike

是否可以从 .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 方法?

最佳答案

Is it possible to call a Web Api method from a .NET 2.0 client?

当然可以。您绝对可以从任何 HTTP 兼容的客户端调用它。客户端甚至可能不是 .NET。

例如,在 .NET 2.0 中,您可以使用 WebClient类:

using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.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);
//now use a JSON parser to parse the resulting string back to some CLR object
}

关于c# - 从 .NET 2.0 客户端调用 Web Api 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com