Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call web APIs in C# using .NET framework 3.5

Tags:

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.

like image 362
sdwaraki Avatar asked Mar 14 '14 06:03

sdwaraki


People also ask

What is Web API call?

What is Web API? API stands for Application Programming Interface. A Web API is an application programming interface for the Web. A Browser API can extend the functionality of a web browser. A Server API can extend the functionality of a web server.

How do you consume Web API?

To consume Web API in ASP.NET MVC server side we can use HttpClient in the MVC controller. HttpClient sends a request to the Web API and receives a response. We then need to convert response data that came from Web API to a model and then render it into a view.


1 Answers

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\": \"[email protected]\",\"Password\": \"pass#123\"}"))); 

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

like image 181
Manish Parakhiya Avatar answered Sep 19 '22 00:09

Manish Parakhiya