none
为什么我调用Webclient的DownloadStringCompleted委托,会引发e.Result异常? RRS feed

  • 问题

  •         private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadStringAsync(new Uri("http://localhost/api/restserver.aspx"), UriKind.RelativeOrAbsolute);
            }

            void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Result != null)
                {
                    string xmlResult = e.Result;//这里发生异常
                    MessageBox.Show(xmlResult);
                }
                //MessageBox.Show("读取完成");
            }

    异常:

    + $exception {System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
       at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
       at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
       at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
       --- End of inner exception stack trace ---
       at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
       at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
       --- End of inner exception stack trace ---
       at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
       at System.Net.DownloadStringCompletedEventArgs.get_Result()
       at StoryRead.MainPage.client_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)} System.Exception {System.Reflection.TargetInvocationException}

    2013年11月17日 17:01

答案

  • 很明显 The remote server returned an error: NotFound

    http://localhost/api/restserver.aspx   你这个服务器地址是构建在本地的,但对于WP手机来说不是手机的localhost服务地址,你需要使用一个外网直接能访问的服务器地址来测试才可以。

    • 已建议为答案 Andy_Li_ 2013年11月19日 3:49
    • 已标记为答案 微锋群 2013年11月19日 12:06
    2013年11月19日 2:06

全部回复