【问题标题】:HttpWebResponse does not respondHttpWebResponse 没有响应
【发布时间】:2017-09-25 02:16:05
【问题描述】:

HttpWebRequest 有一些问题。我正在尝试管理具有 WebService 的服务器和具有 CF 2.0 客户端的 Windows CE 6.0 之间的连接,而我的实际目的是检索 Windows CE 机器的外部 IP。我尝试使用HttpWebResponse,但它在通话过程中卡住了。
现在我会更清楚,这是我在 WinCE 机器上运行以获取 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;
        }
    }

那么,我的问题是什么?我不知道为什么它在httpRequest.GetResponse() 通话期间卡住了,知道吗? 我不得不说我在代理下,所以我想出了代理可能会阻止一些请求的想法,是吗?

【问题讨论】:

    标签: c# httpwebrequest compact-framework windows-ce httpwebresponse


    【解决方案1】:

    好的,我想出了这样的东西:

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

    但现在的问题是没有任何信息可以检索。我的意思是,我从 Stream 解析的字符串是 html 页面,而检索的字符串是:

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

    我现在能做什么?

    【讨论】:

    • 如果您的目标是获取服务器 IP 地址,您可以尝试通过 DNS 进行主机名解析。否则,您可以更轻松地执行 ping 而不是 WebRequest 并从那里获取 IP...
    • 不,我的目标是获取连接到服务器的机器的IP。无论如何,现在我正在尝试另一条路线来获得机器和服务器之间以及服务器和另一台 PC 之间的连接。
    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多