3
votes

I work on a project target Windows Phone 7.5 and above.

I use a method to get online image and check the type of the image, if it is gif then I will coonvert it into jpg and bind it to a image control, if jpg and png, just bind with no encoding.

But codes below throws a error very frequently, "The remote server returned an error: NotFound",why? I have already catch the WebException.

public void GetOnlineImageAndReturnJPGStream(Action<Stream, string> callback, string uriString)
        {
            string errorstring = "";
            try
            {
                WebClient wc = new WebClient();
                wc.Headers[HttpRequestHeader.Referer] = "http://www.xici.net";
                wc.AllowReadStreamBuffering = true;
                wc.OpenReadCompleted += (s, e) =>
                {
                    if (e.Error == null && !e.Cancelled)
                    {
                        //check pic type
                        ImageTypeCheck.ImageType incomingIMGType = ImageTypeCheck.getImageType(e.Result);

                        switch (incomingIMGType)
                        {
                            case ImageTypeCheck.ImageType.Gif://if gif 
                                //deal with gif
                            case ImageTypeCheck.ImageType.Null:
                            case ImageTypeCheck.ImageType.Bmp:
                                //deal with bmp
                            case ImageTypeCheck.ImageType.Jpg:
                            case ImageTypeCheck.ImageType.Png:
                                //deal with jpg and png
                        }
                    }
                    else
                    {
                        errorstring = e.Error.Message;
                        callback(e.Result, errorstring);
                    }
                };

                wc.OpenReadAsync(new Uri(uriString, UriKind.Absolute));
            }
            catch (WebException webEx)
            {
                App.ShowToastNotification(webEx.Message);
            }
        }

The unhandle exception is below:

{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.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b_0(Object sendState) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.OpenReadCompletedEventArgs.get_Result() at xicihutong.DataServiceAgent.ServiceAgent.<>c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg)} [System.Net.WebException]: {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.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b_0(Object sendState) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.OpenReadCompletedEventArgs.get_Result() at xicihutong.DataServiceAgent.ServiceAgent.<>c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg)} _className: "System.Net.WebException" _data: null _dynamicMethods: null _exceptionMethod: null _exceptionMethodString: null _helpURL: null _HResult: -2146233079 innerException: {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.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b__0(Object sendState)} _ipForWatsonBuckets: 0 _message: "The remote server returned an error: NotFound." _remoteStackIndex: 0 _remoteStackTraceString: null _source: null _stackTrace: {sbyte[96]} _stackTraceString: null _watsonBuckets: {byte[5616]} _xcode: -532462766 xptrs: 0 Data: {System.Collections.ListDictionaryInternal} HelpLink: null HResult: -2146233079 InnerException: {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.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b__0(Object sendState)} IPForWatsonBuckets: 0 Message: "The remote server returned an error: NotFound." RemoteStackTrace: null Source: "System" StackTrace: " at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()\r\n at System.Net.OpenReadCompletedEventArgs.get_Result()\r\n at xicihutong.DataServiceAgent.ServiceAgent.<>c_DisplayClassa.b_8(Object s, OpenReadCompletedEventArgs e)\r\n at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)\r\n at System.Net.WebClient.OpenReadOperationCompleted(Object arg)" WatsonBuckets: {byte[5616]}

Why? and how to handle it?
unfortunately the error message I post is a Unhandle Exception and told me that our server returns a error, but I thought that I've already catch the 404 error in the Unhandle exception, Why does it throw it anyway?

3
have you set ID_CAP_NETWORKING capability?Damith

3 Answers

1
votes

To get more details about the reason of an exception, check the Status property of the WebException object. Also it can be a certificate problem if your app sends an https request.

1
votes

You can't catch exception in described way because it is raised asynchronously in OpenReadCompleted event handler when you access Result property. In case an error has occurred you can't get a Result so an exception is raised. To handle it put try catch block inside the event handler, but actually to prevent an exception just don't pass a Result to callback in case an error has occurred.

0
votes

debug it and try opening the full address of the image you are downloading in the browser. Maybe there's a missing slash or something in the url.