【问题标题】:HTTPWebRequest.GetResponse() throws connection failure exceptionHTTPWebRequest.GetResponse() 抛出连接失败异常
【发布时间】:2011-06-04 19:01:16
【问题描述】:

我正在尝试创建一个执行 HTTP 的简单应用程序 单击按钮时的请求/响应。这是我得到的整个代码 参考书:

using System.Collections.Generic;   
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
using System.IO;  
using System.Net;  

namespace emulator2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            Uri l_Uri = new Uri("http://www.testing.com");
            HttpWebRequest l_WebReq = (HttpWebRequest)WebRequest.Create(l_Uri);
            HttpWebResponse l_WebResponse =
            (HttpWebResponse)l_WebReq.GetResponse();
            Stream l_responseStream = l_WebResponse.GetResponseStream();
            StreamReader l_SReader = new StreamReader(l_responseStream);
            string resultstring = l_SReader.ReadToEnd();
            Console.WriteLine(resultstring);
        }
    }
}

让我困惑的是,当我移动整个代码块时 到 Windows 应用程序,它工作正常。但是当我在设备上使用它时 应用程序,它只是给我一个错误。以下是详细信息 错误:

System.Net.WebException 未处理 Message="无法建立 连接到网络。” StackTrace:在 System.Net.HttpWebRequest.finishGetResponse() 在 System.Net.HttpWebRequest.GetResponse() 在 emulator2.Form1.button1_Click() 在 System.Windows.Forms.Control.OnClick() 在 System.Windows.Forms.Button.OnClick() 在 System.Windows.Forms.ButtonBase.WnProc() 在 System.Windows.Forms.Control._InternalWnProc() 在 Microsoft.AGL.Forms.EVL.EnterMainLoop() 在 System.Windows.Forms.Application.Run() at emulator2.Program.Main()

这一行的错误点:

HttpWebResponse l_WebResponse = (HttpWebResponse)l_WebReq.GetResponse();

有人知道如何解决这个问题吗?我需要得到这个 很快就解决了..所以非常感谢您提供的任何帮助!谢谢!

【问题讨论】:

    标签: c# mobile web-applications windows-mobile


    【解决方案1】:

    我的猜测是模拟器没有正确的网络连接。在旧的 Windows Mobile 模拟器上建立和运行网络可能会很痛苦(并且在我的经验中是偶然的)。 (在 Windows Phone 7 中很容易。)

    加载 Internet Explorer 并查看 是否可以连接到相同的 URL...

    此外,您并没有处置任何资源(如果此代码确实在参考书中与您编写的完全一样,那么这对这本书来说是一个重要的污点)。例如,您应该处理网络响应:

    using (HttpWebResponse response = ...)
    {
    }
    

    同样,我会按照一般原则亲自处理响应流和流读取器。我怀疑当响应被释放时,流也会被释放——但是释放所有流等是有意义的,除非你知道你需要不释放它们。

    【讨论】:

    • 我正在使用 Windows Mobile 6 Professional 作为模拟器。我无法使其连接到网络。有什么想法吗?
    • @strider:检查所有各种连接设置,包括 Windows Mobile 本身和模拟器设置。我似乎记得这就像告诉它使用计算机的默认网络访问来模拟 NE2000。然后,您需要在模拟器中设置与“Internet”的连接。这些都是相当模糊和遥远的记忆——你可以通过搜索找到更准确的演练。至少你知道暂时不用担心代码了 :)
    【解决方案2】:

    如果您在模拟器中运行,则需要“摇篮”模拟器,然后与 ActiveSync (Win XP) 或 Windows Mobile Device Center(Vista 或 7)建立合作关系。

    这将允许模拟器与 PC 共享网络连接。即使您想从模拟器连接到 PC,您也需要这样做。

    正如 Jon 所说,在 WP7 中您不需要以这种方式建立连接,WP7 模拟器会自动共享主机 PC 的网络连接。

    使用 IE Mobile(在模拟器上)检查设备是否可以连接到站点。

    编辑
    要安装模拟器,在 VS2008 中,从工具菜单中选择“设备模拟器管理器”。选择正在运行的模拟器,右键选择“Cradle”。

    Mobile Device Center 应该会自动启动并询问您是否要建立合作伙伴关系,就像您连接了真实设备一样。

    【讨论】:

    • 我使用的是 Windows 7。如何“对接”模拟器并与 Windows Mobile Device Center 建立合作关系?
    • @strider 答案已更新,包括如何“摇篮”模拟器的详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2012-05-11
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多