简体   繁体   中英

How to get the data with a rest api?

I'm very new to .Net and I'm trying to retrieve the data after a call to a rest method

My function is actually like this:

    private Task<HttpResponse> getUser(string user)
    {
        string url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=" + user + "&api_key=xxx&format=json";
        var client = new HttpClient();

        return client.GetAsync(url).ContinueWith(request =>
            {
                var response = request.Result;
                response.EnsureSuccessStatusCode();
                return response.Content.ReadAsStringAsync().ContinueWith(t =>
                    {
                        var result = new HttpResponseMessage();
                        response.CreateContent(t.Result);
                        return response;
                    });
            }).Unwrap();
    }

But I get this error :

'System.Net.Http.HttpResponseMessage' does not contain a definition for 'CreateContent' and no extension method 'CreateContent' accepting a first argument of type 'System.Net.Http.HttpResponseMessage' could be found (are you missing a using directive or an assembly reference?)

Maybe I just forgot something, but even after some search I didn't find any solution. Are there any alternative ways to solve?

Thanks for the help

I found the solution.

Solution: Add a reference to System.Net.Http.Formatting.dll. This assembly is also available in the C:\\Program Files\\Microsoft ASP.NET\\ASP.NET MVC 4\\Assemblies folder.

The method ReadAsAsync is an extension method declared in the class HttpContentExtensions, which is in the namespace System.Net.Http in the library System.Net.Http.Formatting.

Reflector came to rescue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM