2013-10-25 3 views
0

windows mobile 6.5 compact framework 클라이언트에서 서비스를 호출하려고합니다. 이것은 "전송 연결에서 데이터를 읽을 수 없습니다"라는 예외를 제공합니다.Windows Mobile 6.5 컴팩트 프레임 워크에서 편안한 웹 서비스 호출

내가 사용하는 에뮬레이터는 Windows mobile 6 professional emulator입니다. Dotnet framework 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

나는 코드가 정확히 무엇인지 잘 모릅니다. 웹 요청을하기 전에 속성을 설정해야 할 수도 있습니다. 당신은 내가 명시 적으로 일부 속성 설정을 참조

/// <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/에서 전체 코드를 볼 수 있습니다. 이를 사용하고 웹 요청을 변경하고 비동기 호출을 사용할 수 있습니다.

장치의 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 ":"세인트 갈렌 " "adminCode1": "GS", "postalCode": "9011", "countryCode": "HU", "lng": 17.781944437499998, "placeName": "adminName1" "adminName1": "Győr-Moson-Sopron"}, { "adminName2": "Tromsø", "adminCode2": "1902", "adminCode1": "19", "postalCode": 47.607638900000005, : "9011", "countryCode": "NO", "lng": 18.95508, "placeName": "Tromsø", "ISO3166-2": "10", "lat": 69.6489, "adminName1": "Troms" { "adminCode1": "Z"}, { "postalCode": "L-9011", "countryCode": "LU", "lng": 6.1041667, "placeName": "Ettelbruck", "lat": 49.8475} , "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,"placeName " : "EL MANGRULLO", "lat": - 46.4333333, "adminName1": "SANTA CRUZ"}, { "adminCode1": "Z", "postalCode": "9011", "countryCode": "AR", "lng ": -67.5333333,"placeName ":"CAÑADON ESTHER ","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,"placeName ":"Mornington ","lat ": - 45.87922}, {"postalCode ":"9011 ","countryCode ":"PH ","lng ": 124.7325 , "placeName": "Balinguan", "lat": 9.050317457142857}]}

인터넷 익스플로러 모바일에 연결되어 있지 않으면 연결이 잘못 설정되어 있습니다. WMDC 연결 설정과 '이 컴퓨터가 연결되었습니다'를 확인하십시오. 인터넷에서 자동으로 전환하는 것이 도움이 될 수 있습니다. 내가 사용하는 에뮬레이터를 도킹 한 것으로 가정하고 WMDC 또는 ActiveSync는 에뮬레이터와의 연결을 보여줍니다.

+0

Internet Explorer에서도이 특정 링크를 열 수 없습니다. 그러나 에뮬레이터에서 인터넷에 연결할 수 있습니다. 내 에뮬레이터에서 stackoverflow를 열 수 있습니다. 나는 예외를 위해 구글을 검색했다. "전송 연결로부터 데이터를 읽을 수 없다", 그들은 그것이 MTU와 어떤 관계가 있고 내가 이해할 수없는 일부 레지스트리 편집과 관련이 있다고 말하고있다. http://social.msdn.microsoft.com/Forums/en-US/b0f90f2c-cb07-455d-a740-f34bb66a3912/unable-to-read-data-from-the-transport-connection?forum = netfxcompact –

+0

레지스트리가 바뀌면 문제를 해결할 수 있습니다. 원격 레지스트리 편집기 (예 : MS sdk 또는 CeRegEditor : http://ceregeditor.mdsoft.pl/)를 사용할 수 있습니다. 그런 다음 HKEY_LOCAL_MACHINE \ Comm \ Tcpip \ Parms를 찾고 MTU 값을 1300 바이트 (16 진수로 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); 
     } 
    } 
}