11

Working with ASP.NET 5 on my Mac in Visual Studio Code. I have a RESTful API I need to call and not sure exactly how to do it. I've seen many examples using WebClient, HttpClient, WebRequest and HttpWebRequest.

I think my pain point is the dnxcore50 framework. Can someone please point me in the right direction with some code samples?

Richard.Davenport
  • 1,453
  • 3
  • 16
  • 31
  • Read [this tutorial](http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client) – Triet Doan Oct 01 '15 at 15:31
  • HttpClient is available in .NET Core. This [question](http://stackoverflow.com/questions/27946798/httpclient-in-asp-net-5-0-not-found) is rather old, I suspect that you can get it from NuGet without having to go to the nightly builds now. – mason Oct 01 '15 at 15:38
  • @AnhTriet, I tried that, but what I get to this line: Product product = await response.Content.ReadAsAsync>Product>(); I get this error: 'HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found (are you missing a using directive or an assembly reference?) [dnx451, dnxcore50] – Richard.Davenport Oct 01 '15 at 15:53
  • Please provide your code in the question. – Triet Doan Oct 01 '15 at 16:06
  • It's from the tutorial you sent. – Richard.Davenport Oct 01 '15 at 16:11
  • I recommend https://github.com/hhariri/EasyHttp only thing to point out serializer is pretty slow but not many people actually have serialization as a bottleneck. – Chris Marisic Oct 01 '15 at 17:08
  • Could you provide the error screen shot? I got no error. Have you installed `Web API Client Libraries package`? – Triet Doan Oct 02 '15 at 02:04

3 Answers3

9

Here is an example about how to call a service. Please check the References and using carefully.

One important thing you have to do is install the Web API Client Libraries package: From the Tools menu, select NuGet Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command: Install-Package Microsoft.AspNet.WebApi.Client.

For the full source code, check this link.

Call service

Triet Doan
  • 11,455
  • 8
  • 36
  • 69
4

I'm assuming it's the same way we used to do it prior to ASP .NET 5, so first you install the ASP .NET Web API Client Libraries NuGet package.

With that available, you reference System.Net.Http:

using System.Net.Http;

Then you use it as follows:

using (var httpClient = new HttpClient())
{
    var response1 = await httpClient.GetAsync(url1);
    var response2 = await httpClient.PostAsync(url2);
    var response3 = await httpClient.SendAsync(url3);
}

That just gives you the response. Typically you'll want to look into the content, especially for GET requests. You can do this by:

var content = await response1.Content.ReadAsStringAsync();

That merely gives you the string in the content, so if it's JSON, you probably want to use something like JSON.NET (Newtonsoft.Json) to deserialize it into structured classes.

This is from memory so you might need a little tweak here and there.

Gigi
  • 28,163
  • 29
  • 106
  • 188
  • This question is specifically about .NET Core, which your answer does not address. – mason Oct 01 '15 at 17:18
  • I thought someone said HttpClient is available in .NET Core. – Gigi Oct 01 '15 at 17:34
  • Yes, it is. But it's in different namespace. And a different NuGet Package. And probably an entirely different NuGet feed. – mason Oct 01 '15 at 17:40
  • Also, your code sample shows a bunch of await calls one after the other, and they don't look dependent on each other. A perfect use case for `Task.WaitAll` or `Task.WaitAny`. – mason Oct 01 '15 at 17:43
  • It is just an example showing different uses of httpClient methods. It is by no means meant to be a fine example of concurrently executing code. – Gigi Oct 01 '15 at 22:16
3

To do this I'm using the NuGet feed https://api.nuget.org/v3/index.json

In my project.json I currently have these relevant dependencies and just use the "dnxcore50" framework:

"Microsoft.AspNet.WebApi.Client": "5.2.3",
"System.Net.Http": "4.0.0",
"System.Runtime.Serialization.Xml": "4.0.10"

Then I'm using HttpClient. Right now (beta7) it doesn't work on Linux or OSX because of https://github.com/dotnet/corefx/issues/2155.

opiethehokie
  • 1,862
  • 11
  • 14