2017-01-16 45 views
0

我試圖調用創建方法正確使用WCF,我開始調試的WCF項目和結果如下: enter image description here如何使用WCF服務在Xamarin.Forms便攜式類庫

xamarin者,恕不碼,我用HttpClient圖書館如下:

private async Task DownloadInfo() 
     { 
      var Uri = "http://localhost:10300/RestServiceImpl.svc/json"; 
      var httpClient = new HttpClient(); 
      var json= await httpClient.GetStringAsync(Uri); 
     } 

當我試圖從Xamarin.Forms我得到以下得到json結果: enter image description here

我該怎麼辦?

+1

您需要使用服務器的IP或FQDN而不是「localhost」。 – Jason

+0

這是正確的Jason .thx –

回答

1

看來你正在檢查那裏的任務,這並沒有提供那麼多的信息。你可以嘗試一下這個更結構化的方法。

using (var httpClient = new HttpClient()) 
{ 
      httpClient.BaseAddress = new Uri("http://localhost:10300"); 
      var request = "/RestServiceImpl.svc/json"; 

      var result = await httpClient.GetAsync(request); 

      if (!result.IsSuccessStatusCode) 
       throw new HttpRequestException($"{result.StatusCode} \n {result.Content.ReadAsStringAsync().Result} \n\n {httpClient.BaseAddress}{request}"); 

      var json = await result.Content.ReadAsStringAsync(); 

      Debug.WriteLine(json); 
}