'HttpWebRequest'不包含'GetResponseAsync'

时间:2015-11-20 12:37:10

标签: c# windows-phone-8 windows-runtime async-await httpwebrequest

我是xamarin和visual studio的新手,我从微软那里听过这个tuto:

enter link description here

创建跨平台应用程序,但我收到此错误:

'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 * ?)

这是我的代码,我收到此错误: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;
        }
    }
}

请问如何解决它,我检查了HttpWebRequest文档,但我没有把握好问题 谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

不确定HttpWebRequest - 但是现在推荐的一种新的数据获取方式如下:

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;
    }
}

尝试一下,让我知道它是否有效。