2010-12-09 41 views
0

我正在嘗試開發Windows Phone 7應用程序。此應用程序的一部分包括聯繫API以檢索一些數據。爲此,我正在使用HTTPWebRequest和我寫的一個小幫助類;我調用HTTPHelper的sendAPIRequest,然後檢索結果。(C#)異步HTTPWebRequest未能聯繫服務器

問題在於異步請求啓動後的執行鏈。通過我的調試,我發現BeginGetRequestStream的回調requestCallBack被調用。此回調隨後調用BeginGetResponse及其回調;不幸的是,它的回調(responseCallBack)永遠不會觸發。我確定API所在的服務器永遠不會被聯繫(Apache的日誌中沒有任何內容),執行request.BeginGetResponse之後,調試器顯示一個空的本地列表和一個空的堆棧;回調永遠不會被觸發,程序無處可去。我已經嘗試了所有我能想到的調試類型,但在這裏結束。沒有任何例外情況發生,沒有任何關係 - 程序只是無法聯繫服務器並繼續。

這裏是我使用的代碼: public class HTTPHelper { public string postString { get; set; } public string responseData { get; set; }

private static ManualResetEvent readyToGo = new ManualResetEvent(false); 

    public void sendAPIRequest(string mode, string data) 
    { 
     GenericAPIRequest requestInformation = new GenericAPIRequest { data = data, device_id = getDeviceId(), method = mode }; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.69/api/master.php?source=client"); 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.Method = "POST"; 

     string postData = "device_id=" + getDeviceId() + "&data=" + JsonConvert.SerializeObject(requestInformation) + "&mode=" + mode; 
     postString = postData; 

     request.BeginGetRequestStream(new AsyncCallback(requestCallBack), request); 

     // Stop the thread until we've finished our HTTP request 
     readyToGo.WaitOne(); 
     return; 
    } 
    private void requestCallBack(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     Stream postStream = request.EndGetRequestStream(asynchronousResult); 

     byte[] postBytes = Encoding.UTF8.GetBytes(postString); 

     postStream.Write(postBytes, 0, postString.Length); 
     postStream.Close(); 

     request.BeginGetResponse(new AsyncCallback(responseCallBack), request); 
     return; 
    } 
    private void responseCallBack(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
     Stream streamResponse = response.GetResponseStream(); 
     StreamReader streamRead = new StreamReader(streamResponse); 

     responseData = streamRead.ReadToEnd(); 

     streamResponse.Close(); 
     streamRead.Close(); 
     response.Close(); 

     // Release the thread... 
     readyToGo.Set(); 
     return; 

    } 
    private string getDeviceId() 
    { 
     // Gets the unique device ID, allows us to track the device. 
     byte[] result = null; 
     object uniqueId; 

     if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId)) 
      result = (byte[])uniqueId; 

     return System.BitConverter.ToString(result); 
    } 

任何有關這裏做錯了什麼的建議?

感謝您的任何和所有建議。

回答

2

試着拉出readyToGo.WaitOne()。這將導致線程停止並等待信號。很可能請求需要在該線程上處理,因此不會被調用。

奇怪的是,幾分鐘前我有一個非常類似的重置事件問題,雖然它是用WPF而不是HttpRequests :)。

順便說一句,我有CodePlex上的代碼參考庫,其中包括一個HttpWebRequest幫手(它可能不會直接幫助你,只是想我會提到它)。無論如何你都可以自由地使用它(甚至只需要你喜歡的代碼部分)。該項目名爲BizArk,該類稱爲WebHelper。

+0

這似乎已經做到了。對於如何通知我的MainPage.xaml.cs它可以自由地檢索ResponseData(即,異步請求已完成),您有任何建議嗎?我希望能夠阻止線程直到收到響應,但這顯然不是一個可行的解決方案。 – roswell 2010-12-09 00:56:01