使用 .NET 框架 3.5 在 C# 中调用 Web API [英] Call web APIs in C# using .NET framework 3.5

查看:40
本文介绍了使用 .NET 框架 3.5 在 C# 中调用 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据邮政编码查找最近的商店.我开始知道 yelp 和 Foursquare 提供了执行此操作所需的 API.我正在使用 .NET 3.5 框架.您如何发出 http 请求并处理响应.网络上的大多数解决方案都针对 .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.

这篇关于使用 .NET 框架 3.5 在 C# 中调用 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆