2013-10-25 46 views
0

我試圖從Windows Mobile 6.5緊湊框架客戶端調用服務。這給我例外「無法從傳輸連接讀取數據」。從Windows Mobile 6.5緊湊框架調用寧靜的webservice

我使用的模擬器是Windows Mobile 6專業模擬器。 Dotnet框架3.仿真器連接到互聯網。我可以從互聯網上打開網頁。如果創建一個C#Windows控制檯應用程序,相同的代碼運行良好。請指導。

 WebRequest Webrequest; 
     HttpWebResponse response=null; 

     Webrequest = WebRequest.Create("http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo"); 

     Webrequest.Method = "GET"; 
     Webrequest.ContentType = "html/xml"; 
     try 
     { 
      response = (HttpWebResponse)Webrequest.GetResponse(); 
     }catch(Exception exc){ 
      ShowErrorMessage("Ex : " + exc.Message); 

      return; 
     } 

     Stream streamResponse = response.GetResponseStream(); 
     StreamReader streamReader = new StreamReader(streamResponse); 
     string responseStr = streamReader.ReadToEnd(); 

回答

0

我不知道你的代碼究竟出了什麼問題。可能您必須在發出Web請求之前設置一些屬性。這裏是我以前從網絡服務獲得區信息片段:

/// <summary> 
    /// This will call the webservice asynchronousely 
    /// the call will immediately return 
    /// The answer on the request is published to all event subscribers 
    /// </summary> 
    /// <param name="lat">decimal input for geographic Latitude</param> 
    /// <param name="lng">decimal input for geographic Longitude</param> 
    public void startRequest(string lat, string lng) 
    { 
     //tzInfos pTZinfos = new tzInfos(); 
     string myURL = @"http://ws.geonames.org/" + _webMethodName; 
     //HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myURL); 
     //HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); 
     //string wsdl = ""; 
     _webServiceURI = myURL; 
     try 
     { 
      _httpRequest = (HttpWebRequest)HttpWebRequest.Create(this._webServiceURI + "?lat=" + lat + "&lng=" + lng); // "?WSDL"); 
      _httpRequest.Method = "POST"; 
      _httpRequest.KeepAlive = false; 
      _httpRequest.ContentType = "application/x-www-form-urlencoded"; 
      //httpRequest.ContentLength = requestData.Length; 
      _httpRequest.Timeout = 30000; 

      //async call 
      IAsyncResult myAsycnCall = this._httpRequest.BeginGetResponse(new AsyncCallback(this.ResponseReceivedHandler), null);     
     } 
     catch (WebException wx) 
     { 
      System.Diagnostics.Debug.WriteLine("Exception: " + wx.Message); 
     } 
     catch (Exception wx) 
     { 
      System.Diagnostics.Debug.WriteLine("Exception: " + wx.Message); 
     } 
    } 
    /// <summary> 
    /// This is the async method for the response to be get from the web service 
    /// </summary> 
    /// <param name="result"></param> 
    private void ResponseReceivedHandler(IAsyncResult result) 
    { 
     string wsdl = ""; 
     geonamesTZfields pTZinfos = new geonamesTZfields(); 
     geonamesEventArgs tArg = new geonamesEventArgs(); 
     try 
     { 
      HttpWebResponse response = (HttpWebResponse)this._httpRequest.EndGetResponse(result); 

      System.IO.Stream streamResponse = response.GetResponseStream(); 
      System.IO.StreamReader streamRead = new System.IO.StreamReader(streamResponse); 
      wsdl = streamRead.ReadToEnd(); 
      System.Diagnostics.Debug.WriteLine("Response: " + wsdl); 

      // Close the stream object 
      streamResponse.Close(); 
      streamRead.Close(); 

      // Release the HttpWebResponse 
      response.Close(); 
      if (wsdl.Length > 0) 
      { 
       StringBuilder sb = new StringBuilder(wsdl); 
       pTZinfos.strCountryCode = xml_helper.xml_helper.getStrSetting(sb, "countryCode"); 
       pTZinfos.strCountryName = xml_helper.xml_helper.getStrSetting(sb, "countryName"); 
       pTZinfos.strTimezoneID = xml_helper.xml_helper.getStrSetting(sb, "timezoneId"); 
       pTZinfos.gmtOffset = xml_helper.xml_helper.getDecimalSetting(sb, "gmtOffset"); 
       pTZinfos.rawOffset = xml_helper.xml_helper.getDecimalSetting(sb, "rawOffset"); 
       pTZinfos.dstOffset = xml_helper.xml_helper.getDecimalSetting(sb, "dstOffset"); 
       pTZinfos.tzTime = xml_helper.xml_helper.getDateTimeSetting(sb, "time"); 
      } 
      _tzFields = pTZinfos; 
      //fire the event 
      tArg.m_myEventArgumentdata = pTZinfos; 
      tArg.m_myEventArgumentdata.DataIsValid = true; 
      tArg.m_myEventArgumentdata.strLastError = "no error"; 
     } 
     catch (WebException wx) 
     { 
      tArg.m_myEventArgumentdata.DataIsValid = false; 
      tArg.m_myEventArgumentdata.strLastError = wx.Message; 
     } 
     catch (Exception ex) 
     { 
      tArg.m_myEventArgumentdata.DataIsValid = false; 
      tArg.m_myEventArgumentdata.strLastError = ex.Message; 
     } 
     finally 
     { 
      this.onDataReceived(tArg); //notify all subscribers of new data arrived 
     } 
    } 

你看,我明確地設置一些屬性: _httpRequest.Method =「POST」; _httpRequest.KeepAlive = false; _httpRequest.ContentType =「application/x-www-form-urlencoded」; //httpRequest.ContentLength = requestData.Length; _httpRequest.Timeout = 30000; 完整的代碼可在我的文章中找到:http://www.hjgode.de/wp/2010/03/04/using-ws-geonames-org-timezone-webservice-without-wsdl/。您可以使用它並更改您的Web請求並使用異步調用。

您也可以嘗試在設備上的Internet Explorer Mobile中簡單打開網頁:http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo。你應該得到的答案是:

{「postalCodes」:[{「adminCode3」:「3203」,「adminName2」:「Wahlkreis St. Gallen」,「adminName3」:「St。Gallen」,「adminCode2 「:」1721「,」adminCode1「:」SG「,」postalCode「:」9011「,」countryCode「:」CH「,」lng「:9.399845287638328,」placeName「:」St. Gallen「,」lat「 「adminName1」:「Kanton St. Gallen」},{「adminCode1」:「GS」,「postalCode」:「9011」,「countryCode」:「HU」,「lng」:17.781944437499998,「placeName」傑爾」, 「LAT」:47.607638900000005 「adminName1」: 「傑爾 - 莫鬆 - 肖普朗」},{ 「adminName2」: 「特羅姆瑟」, 「adminCode2」: 「1902」, 「adminCode1」: 「19」, 「POSTALCODE」 : 「9011」, 「COUNTRYCODE」: 「NO」, 「LNG」:18.95508, 「地名」: 「特羅姆瑟」, 「ISO3166-2」: 「10」, 「LAT」:69.6489 「adminName1」: 「特羅姆斯」 },{ 「POSTALCODE」: 「L-9011」, 「COUNTRYCODE」: 「LU」, 「LNG」:6.1041667, 「地名」: 「的Ettelbruck」, 「LAT」:49.8475},{ 「adminCode1」: 「Z」 ,「postalCode」:「9011」,「countryCode」:「AR」,「lng」: - 67.5333333,「placeName」:「CALETA OLIVIA」,「lat 「:-46.4333333,」adminName1「:」SANTA CRUZ「},{」adminCode1「:」Z「,」postalCode「:」9011「,」countryCode「:」AR「,」lng「: - 67.5333333, :「EL MANGRULLO」,「lat」: - 46.4333333,「adminName1」:「SANTA CRUZ」},{「adminCode1」:「Z」,「postalCode」:「9011」,「countryCode」:「AR」 「:-67.5333333,」placeName「:」CAÑADONESTHER「,」lat「: - 46.4333333,」adminName1「:」SANTA CRUZ「},{」adminCode1「:」Z「,」postalCode「:」9011「,」countryCode 「:」AR「,」lng「: - 67.5333333,」placeName「:」BAHIA LANGARA「,」lat「: - 46.4333333,」adminName1「:」SANTA CRUZ「},{」postalCode「:」9011「,」countryCode 「:」 NZ」, 「LNG」:170.47198, 「地名」: 「莫寧」, 「LAT」: - 45.87922},{ 「POSTALCODE」: 「9011」, 「COUNTRYCODE」: 「PH」, 「LNG」:124.7325 ,「placeName」:「Balinguan」,「lat」:9.050317457142857}]}

如果Internet Explorer Mobile中沒有這樣的連接,則連接設置不正確查找WMDC連接設置並且此計算機連接到'。從Internet切換到自動可能會有所幫助。我假設你已經停靠了你使用的模擬器,WMDC或ActiveSync顯示了與模擬器的連接。

+0

我無法在Internet Explorer中打開此特定鏈接。但是,我可以從模擬器連接到Internet。我可以在我的模擬器上打開stackoverflow。我搜索谷歌的異常「無法從傳輸連接讀取數據」,他們說它與手機的MTU和某些我無法理解的註冊表編輯有關。 http://social.msdn.microsoft.com/Forums/en-US/b0f90f2c-cb07-455d-a740-f34bb66a3912/unable-to-read-data-from-the-transport-connection?論壇= netfxcompact –

+0

如果註冊表更改解決您的問題,只需給他們一個嘗試:您可以使用遠程註冊表編輯器(如與MS sdk或CeRegEditor一起提供:http://ceregeditor.mdsoft.pl/)。然後查找HKEY_LOCAL_MACHINE \ Comm \ Tcpip \ Parms並將MTU的值設置爲1300字節(514爲十六進制),並添加DOWRD值EnablePMTUDiscovery,並將值設置爲0.您上面提供的鏈接還包含一些.NET代碼以更改代碼的reg值。 – josef

+0

感謝您的信息。這個問題現在已經解決了,沒有任何改變。沒有註冊表修改和代碼改變。我不知道爲什麼它現在正在工作...請看看我的另一個問題,並留下您的迴應,如果你有一些知識。 –

0
class ServiceConnection 
{ 
    public string Url = ""; 

    private string postXml; 

    public delegate void OnServerEndResponse(string response, int statusCode); 
    public event OnServerEndResponse OnEndResponse; 
    public ServiceConnection(string serviceName, string userName, string pwd) 


    public void SendRequest(string requestXml) 
    { 
     postXml = requestXml; 
     StartWebService(); 
    } 

    public void StartWebService() 
    { 
     WebRequest request = WebRequest.Create(Url); 
     request.Method = "GET"; 
     byte[] auth = Encoding.UTF8.GetBytes(UserName + ":" + Pwd); 
     request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(auth); 
     request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
    } 

    void GetResponseCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebResponse response = null; 
     HttpWebRequest webRequest = null; 

     try 
     { 
      webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamReader = new StreamReader(streamResponse); 
      StringBuilder sb = new StringBuilder(); 
      sb.Append(streamReader.ReadToEnd()); 
      //string serverResponse = streamReader.ReadToEnd(); 
      streamResponse.Close(); 
      streamReader.Close(); 
      response.Close(); 


      OnEndResponse(sb.ToString(), Convert.ToInt32(response.StatusCode)); 
     } 
     catch (Exception ex) 
     { 
      if (ex.Message == "The remote server returned an error: (401) Unauthorized.") 
       OnEndResponse(ex.Message, 401); 
      else if (ex.Message == "Unable to connect to the remote server") 
       OnEndResponse(ex.Message, 4); 
      else if (ex.Message == "Could not establish connection to network.") 
       OnEndResponse(ex.Message, 10000); 
      else 
       OnEndResponse("Error in network", 10001); 
     } 
    } 
}