HttpClient任务无法启动

时间:2014-01-24 18:51:03

标签: c# .net windows-phone-7

我有一些非常简单的代码,我一直在Windows 8商店应用程序中使用没有任何问题。但是,当我与Windows手机应用程序共享代码时它只是永远挂起。见下面的示例。这将调用任何Web URL并将其源作为字符串返回。 post和get方法都以相同的方式挂起。非常感谢任何帮助,谢谢。

    public static string GetWebSource(string Url)
    {
        HttpClient client = new HttpClient();
        Task<HttpResponseMessage> Resp = client.GetAsync(Url);

        //code hangs on following line forever
        //Resp.status always stays at waiting for activation
        Task.WaitAll(Resp);

        if (Resp.Result.IsSuccessStatusCode)
        {
            Task<string> response = Resp.Result.Content.ReadAsStringAsync();
            Task.WaitAll(response);
            return response.Result;
        }
        return "";
    }

    public static string PostWebSource(string Url, string data)
    {
        HttpClient client = new HttpClient();
        StringContent sc = new StringContent(data);
        Task<HttpResponseMessage> Resp = client.PostAsync(Url, sc);

        //code hangs on following line forever
        //Resp.status always stays at waiting for activation
        Resp.Wait();

        if (Resp.Result.IsSuccessStatusCode)
        {
            Task<string> response = Resp.Result.Content.ReadAsStringAsync();
            Task.WaitAll(response);
            return response.Result;
        }
        return "";
    }

1 个答案:

答案 0 :(得分:0)