hanali
hanali

Reputation: 227

'HttpWebRequest' does not contain a definition for 'GetResponseAsync'

I am new in xamarin and visual studio,I have followed this tuto from microsoft:

enter link description here

to create a cross platform application,but I get this error:

'HttpWebRequest' does not contain a definition for 'GetResponseAsync' and no extension method 'GetResponseAsync' accepting a first argument of type 'HttpWebRequest' was found (a using directive or an assembly reference is it missing * ?)

and this is my code in which I get this error:DataService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace shared
{
    //This code shows one way to process JSON data from a service

    public class DataService
    {
        public static async Task<dynamic> getDataFromService(string queryString)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(queryString);

            var response = await request.GetResponseAsync().ConfigureAwait(false);
            var stream = response.GetResponseStream();

            var streamReader = new StreamReader(stream);
            string responseText = streamReader.ReadToEnd();

            dynamic data = JsonConvert.DeserializeObject(responseText);

            return data;
        }
    }
}

Please how can I solve it, I checked the HttpWebRequest documentation but I didn't get well the problem thanks for help

Upvotes: 1

Views: 2564

Answers (1)

leonard_deutsch
leonard_deutsch

Reputation: 361

Not sure about HttpWebRequest - but a newer and now recommended way to get data is the following:

public static async Task<dynamic> getDataFromService(string queryString)
{
    using (var client = new HttpClient())
    {
         var responseText = await client.GetStringAsync(queryString);
         dynamic data = JsonConvert.DeserializeObject(responseText);
         return data;
    }
}

Try that and let me know if it works.

Upvotes: 2

Related Questions