2014-01-24 129 views
0

我有一些非常簡單的代碼,我一直在Windows 8商店應用程序中使用沒有任何問題。但是,當我與Windows Phone應用程序共享代碼它只是永遠掛起。見下面的示例。這會調用任何網址並以字符串的形式返回其源。帖子和get方法都以相同的方式掛起。任何幫助非常感謝,謝謝。HttpClient任務無法啓動

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

回答