2014-12-02 76 views
0

我已經寫在Visual Studio中一個簡單的Web服務2012年Xamarin表格 - 消費用Visual Studio中的Web服務2012

當我測試在Internet Explorer中的Web服務,我得到以下結果。

<?xml version="1.0" encoding="UTF-8"?> 
<string xmlns="http://tempuri.org/">[{"ID":1,"ConsultantName":"Jim", 
"ConsultantEmailAddress":"[email protected]","ConsultantCellNumber":null, 
"ConsultantPhoneNumber":null},{"ID":2,"ConsultantName":"Steve","ConsultantEmailAddress":"[email protected]", 
"ConsultantCellNumber":null,"ConsultantPhoneNumber":null}]</string> 

兩個記錄以JSON格式返回

一切看起來很好

所以現在來調用XAMARIN Web服務:

  1. 了大量的研究,我發現,「後LOCALHOST「在ANDROID世界中沒有任何意義 - 所以我用10.0.2.2替換了」LOCALHOST「,並且執行了更多的代碼。
  2. 我看了所有的例子我能找到使用Web服務,並最終在下面的修改後的代碼解決:

在下面的代碼它好像然而,當調用Web服務時請求成功該代碼嘗試使用結果(JSON字符串)發生400錯誤請求錯誤。

這是發生錯誤的行:

HttpWebResponse響應=(HttpWebResponse)request.EndGetResponse(callbackResult);

(這條線是正確的代碼的包括底部)

錯誤 - //遠程服務器返回錯誤:(400)錯誤的請求

的代碼如下: 我將不勝感激任何評論或指導。

除此之外,如果XAMARIN能夠提供一個真正簡單的Web服務消費示例,那將會很棒。我目前看到的每一個例子都有複雜性,如果你的知識很少,你甚至在能夠理解代碼之前必須做大量的研究。 如何使用非常簡單的代碼覆蓋以下視頻:使用XAMARIN Forms進行異步Web消費。 我面臨的問題是我花了三天的時間擺弄。如果我有一個簡單的工作例子,我可以慢慢增加複雜性,直到我完全理解正在發生的事情,但現在我仍然不知道我的問題在哪裏。

謝謝您的期待。

private async Task ExecuteLoadConsultants() 
    { 

     Initialized = true; 
     IsBusy = true; 

     ConsultantInfo x = new ConsultantInfo{ ConsultantName = "WEwewewewe" }; 
      Consultants.Add (x); 

     // call to webservice 

      HttpWebRequest webRequest = 
         (HttpWebRequest)WebRequest.Create ("http://10.0.2.2:53498/TennantMobileWS1.asmx?op=GetConsultantAll"); 
     webRequest.UseDefaultCredentials = true; 
     webRequest.Method = "POST"; 
     webRequest.Accept = "application/json"; 
     webRequest.ContentType = "application/json"; 
     webRequest.UseDefaultCredentials = true; 

     webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest); 

     IsBusy = false; 
    } 



    private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 

     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
     // End the stream request operation 

     Stream postStream = request.EndGetRequestStream(asynchronousResult); 

     //Start the web request 
     request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request); 

    } 

    void GetResponceStreamCallback(IAsyncResult callbackResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState; 


     // Remote server returned an error: (400) Bad Request on line below 


     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult); 
     Stream streamResponse = response.GetResponseStream(); 
     StreamReader streamRead = new StreamReader(streamResponse); 
     string responseString = streamRead.ReadToEnd(); 

    } 

回答

0

使用ngrok一條隧道,並獲得本地主機訪問和酒吧要求做到這一點:

點擊「開始按鈕」 在搜索框中輸入「啓用Windows功能打開或關閉「 」在功能窗口中,點擊:「Internet Information Services」 點擊:「萬維網服務」 點擊:「應用程序開發功能」 檢查(啓用)功能。我檢查了所有CGI。

我得到了形式here

我希望這可以幫助您:)

貸到相應的