简体   繁体   中英

HttpWebResponse does not respond

I've some issue with the HttpWebRequest . I'm trying to manage a connection between a server with a WebService and a Windows CE 6.0 with CF 2.0 client and my actual purpose is to retrieve the external IP of the Windows CE machine. I've tried to using the HttpWebResponse but it get stuck during a call.
Now I'll be more clearly, that's the code that I'm running on the WinCE machine to get the IP:

    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;
        }
    }

So, what's my problem? I don't know why it stucks during the httpRequest.GetResponse() call, any idea? I've to say that I'm under a proxy, so I came up with the idea that maybe the proxy blocks some requests, could it be?

Ok, I came up with something like this:

    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 "";
    }

But now the problem is that there aren't any information to retrieve. I mean, the string that I parse from the Stream is a html page and the string that retrieve is:

<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>

What can I do now?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM