使用.NET Framework 3.5在C#中调用Web API的Framework、NET、API、Web

2023-09-07 02:32:26 作者:致我们逝去的青春

我想找到给定一个zip code最近的商店。我才知道,Yelp和Foursquare的提供所需的API来做到这一点。我使用.NET 3.5框架。如何做才能让你的HTTP请求和处理网络上的solns的responses.Most给它的.NET 4.5起,其中包括了HTTPClient类的用法。

I am trying to find the nearest store given a zip code. I came to know that yelp and foursquare provides the required APIs to do this. I am using .NET 3.5 framework. How do you make the http requests and handle the responses.Most of the solns on the web give it for .NET 4.5 onwards which includes the usage of HTTPClient class.

推荐答案

您可以使用System.Net.WebClient类进行HTTP请求。

You can use System.Net.WebClient class to make an http request.

 System.Net.WebClient client = new System.Net.WebClient();
 client.Headers.Add("content-type", "application/json");//set your header here, you can add multiple headers
 string s = Encoding.ASCII.GetString(client.UploadData("http://localhost:1111/Service.svc/SignIn", "POST", Encoding.Default.GetBytes("{\"EmailId\": \"admin@admin.com\",\"Password\": \"pass#123\"}")));

有也其他方法可被使用,但取决于您的要求。你可以找到更多的细节这里 从MSDN。

There are also other methods which can be used, but that depends on your requirements. You can find more details Here from MSDN.