0

HttpWebRequest과 관련된 문제가 있습니다. WebService가있는 서버와 CF 2.0 클라이언트가있는 Windows CE 6.0 사이의 연결을 관리하려고합니다. 실제 목적은 Windows CE 컴퓨터의 외부 IP를 검색하는 것입니다. 나는 HttpWebResponse을 사용하려고했지만 통화 도중 멈춰 버립니다.HttpWebResponse가 응답하지 않습니다.

private string GetIPAddressRemote() 
    { 
     Uri validUri = new Uri("http://icanhazip.com"); 

     try 
     { 
      string externalIP = ""; 

      HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(validUri); 

      httpRequest.Credentials = CredentialCache.DefaultCredentials; 
      httpRequest.Timeout = 10000; // Just to haven't an endless wait 

      using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) 
      /* HERE WE ARE 
      * In this point my program stop working... 
      * well actually it doesn't throw any exception and doesn't crash at all 
      * For that reason I've setted the timeout property because in this part 
      * it starts to wait for a response that doesn't come 
      */ 
      { 
       using (Stream stream = httpResponse.GetResponseStream()) 
       { 
        // retrieve the return string and 
        // save it in the externalIP variable 
       } 
      } 

      return externalIP; 
     } 
     catch(Exception ex) 
     { 
      return ex.Message; 
     } 
    } 

그래서, 내 문제가 무엇 :
가 지금은 더 명확하게 할 것, 그게 내가이 IP를 얻기 위해 주춤 컴퓨터에서 실행하고있어 코드는? 나는 왜 그것이 httpRequest.GetResponse() 전화, 어떤 생각하는 동안 붙어 있는지 모르겠다? 나는 프록시하에 있다고 말했기 때문에 프록시가 일부 요청을 차단했다는 생각이 들었다.

답변

0

좋아, 나는 이런 식으로 뭔가를 내놓았다 :

private string GetIPAddressRemote() 
    { 
     Uri validUri = new Uri("http://icanhazip.com/"); 

     int tryNum = 0; 

     while (tryNum < 5) 
     { 
      tryNum++; 

      try 
      { 
       string externalIP = ""; 

       WebProxy proxyObj = new WebProxy("http://myProxyAddress:myProxyPort/", true); // Read this from settings 

       WebRequest request = WebRequest.Create(validUri); 

       request.Proxy = proxyObj; 
       request.Credentials = CredentialCache.DefaultCredentials; 
       request.Timeout = 10000; // Just to haven't an endless wait 

       using (WebResponse response = request.GetResponse()) 
       { 
        Stream dataStream = response.GetResponseStream(); 

        using (StreamReader reader = new StreamReader(dataStream)) 
        { 
         externalIP = reader.ReadToEnd(); 
        } 
       } 
       return externalIP; 
      } 
      catch (Exception ex) 
      { 
       if(tryNum > 4) 
        return ex.Message; 
      } 

      Thread.Sleep(1000); 
     } 
     return ""; 
    } 

하지만 지금 문제는 검색 할 정보가없는 것입니다.

<html> 
    <body> 
    <h1>It works!</h1> 
    <p>This is the default web page for this server.</p> 
    <p>The web server software is running but no content has been added, yet.</p> 
    </body> 
</html> 

내가 지금 할 수있는 일은 무엇인가 : 나는 내가 Stream에서 구문 분석 문자열이 html 페이지 검색은 문자열입니다 의미?

+0

목표가 서버 IP 주소를 얻는 것이라면 DNS를 사용하여 호스트 이름 확인을 시도 할 수 있습니다. 그렇지 않으면 WebRequest 대신 ping을 실행하고 거기에서 IP를 얻는 것이 더 쉽습니다. – salvolds

+0

아니요, 제 목표는 서버에 연결된 시스템의 IP를 얻는 것입니다. 어쨌든 이제 나는 기계와 서버 사이의 연결을 얻고 서버와 다른 PC 사이의 연결을 얻는 또 다른 경로를 찾으려고합니다. –

관련 문제