I'm building a wp7 app. I'm using WebClient
to grab data from a server. In the past, it's been working, although all of a sudden it's failing.
static void downloadData(string uri, Action<object, DownloadStringCompletedEventArgs> onComplete)
{
Debug.WriteLine("Downloading: " + uri);
WebClient data = new WebClient();
data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(onComplete);
data.DownloadStringAsync(new Uri(uri));
}
static void data_SectionDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
// throws NotFound
throw e.Error;
}
// ...
}
When I go to the URI 开发者_StackOverflow社区in question in my browser, it works fine.
The exception:
"The remote server returned an error: NotFound." {System.Net.WebException}
What am I doing wrong here?
Update: I restarted the emulator, and now it works fine. Weird. Maybe it's an issue in the emulator? Hopefully I won't be able to reproduce it on the actual device.
NotFound is a generic error that basically means 'Error'. There is a real exception behind it that you need to dig to find. The easiest method I have found is Intellitrace which allows you to view every exception that ever happened on your web server. If you look just before your NotFound, you will find the real exception that backs it.
If Intellitrace is not an option, add more/better logging on your server and client. Google has many tips, a good example of deeper debugging:
http://www.mostlydevelopers.com/blog/post/2009/01/14/debugging-tips-ndash3b-the-remote-server-returned-an-error-notfound.aspx
I had similar experience with the emulator. I often open Internet Explorer and browse a site before I test any application that uses the network. Also, it is best not to change the IP address of the machine running the emulator, DHCP or manually. Lastly, I would suggest you handle any error scenarios with an error message displayed in a MessageBox.
HTH, indyfromoz
I get this occassionaly too. Even on real devices. A retry usually fixes it though.
Unfortunately this is one of the issues you need to be aware of and write code to cater for when working in an occassionally connected environment.
精彩评论