-1

I've searched some time, looking for easy way to connect with some other sites WebAPI. There are some solutions, but they are made in very complicated way.

What I want to do:

  1. Connect with server using URL adress
  2. Provide login and password to get some data
  3. Get data as JSON/XML
  4. Save this data in an "easy-to-read" way. I mean: save it to C# variable which could be easy to modify.

Currently, API that I want to work with is Bing Search, but I'm looking for some universal way. I found an example, but it doesn't work for me and in my app I can't use this class: "DataServiceQuery" because it doesn't exsist.

How do you usually do it? Do you have your favourite solutions? Are there some universal ways or it depends on type of API that you work with?

I'm currently working on .NET MVC app (in case it could make any difference)

Piotrek
  • 10,919
  • 18
  • 73
  • 136

2 Answers2

1

From server side

You can use that like below.

// Create an HttpClient instance 
HttpClient client = new HttpClient(); 

// Send a request asynchronously continue when complete 
client.GetAsync(_address).ContinueWith( 
      (requestTask) => 
     { 
          // Get HTTP response from completed task. 
          HttpResponseMessage response = requestTask.Result; 

          // Check that response was successful or throw exception 
          response.EnsureSuccessStatusCode(); 

           // Read response asynchronously as JsonValue
          response.Content.ReadAsAsync<JsonArray>().ContinueWith( 
                (readTask) => 
                { 
                    var result = readTask.Result
                    //Do something with the result                   
                }); 
     }); 

You can see example on following link.

https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee

For JavaScirpt: You could use jQuery and WebAPI both together to do your stuff.

There are few steps to it.

  1. Call web api with Ajax jquery call.
  2. Get reponse in JSON
  3. Write javascript code to manipulate that response and do your stuff.

This is the easiest way.

See following link for reference:

http://www.codeproject.com/Articles/424461/Implementing-Consuming-ASP-NET-WEB-API-from-JQuery

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
0

It entirely depends on the type of API you want to use. From a .Net point of view, there could be .Net 2 Web Services, WCF Services and Web API Services.

Web APIs today are following the REST standard and RMM. Some APIs need API Keys provided as url parameters, others require you to put in request's header. Even some more robust APIs, use authentication schemes such as OAuth 2. And some companies have devised their own standards and conventions.

So, the short answer is that there is no universal way. The long answer comes from documentation of each API and differs from one to another.

Delphi.Boy
  • 1,199
  • 4
  • 17
  • 38