简体   繁体   中英

HTTPWebRequest.GetResponse() throws connection failure exception

I'm trying to create a simple application that does a HTTP request/response on a button click. Here's the entire code which I got from a reference book:

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

The thing that puzzles me is that when I shift the entire chunk of code to a Windows Application, it works fine. But when I use it on a Device Application, it just throws me an error. Here are the details of the error:

System.Net.WebException was unhandled Message="Could not establish connection to network." StackTrace: at System.Net.HttpWebRequest.finishGetResponse() at System.Net.HttpWebRequest.GetResponse() at emulator2.Form1.button1_Click() at System.Windows.Forms.Control.OnClick() at System.Windows.Forms.Button.OnClick() at System.Windows.Forms.ButtonBase.WnProc() at System.Windows.Forms.Control._InternalWnProc() at Microsoft.AGL.Forms.EVL.EnterMainLoop() at System.Windows.Forms.Application.Run() at emulator2.Program.Main()

The error points at this line:

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

Does anyone have any idea on how to solve this? I need to get this solved real quick..so any help given is greatly appreciated! Thanks!

My guess is that the emulator doesn't have proper network connectivity. It can be a pain (and hit-and-miss in my experience) getting networking up and running on the old Windows Mobile emulators. (It's easy in Windows Phone 7.)

Load up Internet Explorer and see if that can make a connection to the same URL...

Additionally, you're not disposing of any of your resources (and if this code is really in a reference book exactly as you wrote it, that's a significant black mark against the book). For example, you should be disposing of the web response:

using (HttpWebResponse response = ...)
{
}

Likewise I would personally dispose of the response stream and the stream reader, just on general principle. I suspect that when the response is disposed, the stream will be too - but it makes sense to dispose of all streams etc unless you know you need to leave them undisposed.

If you're running in the emulator you'll ned to "cradle" the emulator and then create a partnership with ActiveSync (Win XP) or Windows Mobile Device Center (Vista or 7).

This will allow the emulator to share the network connection with the PC. You also need to do this even if you want to connect from the emulator to the PC.

As Jon mentions, in WP7 you don't need to make a connection in this way, the WP7 emulator automatically shares the network connection of the host PC.

Use IE Mobile (on the emulator) to check that the device can connect to the site.

Edit
To cradle the emulator, in VS2008 select "Device Emulator Manager" from the Tools menu. Select the emulator that's running, right click and select "Cradle".

Mobile Device Center should start automatically and ask if you want to create a partnership, just as if you'd connected a real device.

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