Наследование: System.Exception
Пример #1
0
        /// <summary>
        /// The asynchronous web response callback.
        /// </summary>
        /// <param name="asyncResult">The asynchronous result.</param>
        /// <param name="callback">The callback method.</param>
        /// <param name="state">The asynchronous state.</param>
        private static void ResponseCallback(IAsyncResult asyncResult, Action <string, Exception> callback, object state)
        {
            string    result    = null;
            Exception exception = null;

            try
            {
                var request  = (HttpWebRequest)asyncResult.AsyncState;
                var response = (HttpWebResponse)request.EndGetResponse(asyncResult);

                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            catch (WebException ex)
            {
                exception = new WebExceptionWrapper(ex);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (callback != null)
                {
                    callback(result, exception);
                }
            }
        }
        /// <summary>
        /// Gets the response string from web exception.
        /// </summary>
        /// <param name="webException">
        /// The web exception.
        /// </param>
        /// <returns>
        /// Response string.
        /// </returns>
        internal static string GetResponseString(WebExceptionWrapper webException)
        {
            if (webException.HasResponse)
            {
                using (var stream = webException.GetResponseStream())
                {
                    if (stream != null)
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            return(reader.ReadToEnd());
                        }
                    }
                }
            }

            return(null);
        }
Пример #3
0
        internal static FacebookApiException GetGraphException(WebExceptionWrapper exception)
        {
            Contract.Requires(exception != null);

            FacebookApiException resultException = null;

            try
            {
                if (exception.HasResponse)
                {
                    object response = null;
                    string json     = null;
                    using (var stream = exception.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                json = reader.ReadToEnd();
                            }
                        }
                    }

                    if (json != null)
                    {
                        response = JsonSerializer.Current.DeserializeObject(json);
                    }

                    resultException = GetGraphException(response);
                }
            }
            catch
            {
                resultException = null;

                // We dont want to throw anything associated with
                // trying to build the FacebookApiException
            }

            return(resultException);
        }
Пример #4
0
        internal static FacebookApiException GetGraphException(WebExceptionWrapper exception)
        {
            Contract.Requires(exception != null);

            FacebookApiException resultException = null;
            try
            {
                if (exception.HasResponse)
                {
                    object response = null;
                    string json = null;
                    using (var stream = exception.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                json = reader.ReadToEnd();
                            }
                        }
                    }

                    if (json != null)
                    {
                        response = JsonSerializer.Current.DeserializeObject(json);
                    }

                    resultException = GetGraphException(response);
                }
            }
            catch
            {
                resultException = null;

                // We dont want to throw anything associated with 
                // trying to build the FacebookApiException
            }

            return resultException;
        }
Пример #5
0
        public WebClientWrapper(WebClient webClient)
        {
            _webClient = webClient;

#if SILVERLIGHT
            _webClient.UploadStringCompleted +=
#else
            _webClient.UploadDataCompleted +=
#endif
                (o, e) =>
            {
                if (UploadDataCompleted == null)
                {
                    return;
                }

                if (e == null)
                {
                    UploadDataCompleted(o, null);
                }
                else
                {
                    byte[] result = null;
                    var    error  = e.Error;

                    if (error == null)
                    {
#if SILVERLIGHT
                        if (!string.IsNullOrEmpty(e.Result))
                        {
                            result = System.Text.Encoding.UTF8.GetBytes(e.Result);
                        }
#else
                        result = e.Result;
#endif
                    }
                    else if (error is WebException)
                    {
                        error = new WebExceptionWrapper((WebException)error);
                    }

                    UploadDataCompleted(o, new UploadDataCompletedEventArgsWrapper(error, e.Cancelled, e.UserState, result));
                }
            };

#if SILVERLIGHT
            _webClient.DownloadStringCompleted +=
#else
            _webClient.DownloadDataCompleted +=
#endif

                (o, e) =>
            {
                if (DownloadDataCompleted == null)
                {
                    return;
                }

                if (e == null)
                {
                    DownloadDataCompleted(o, null);
                }
                else
                {
                    byte[] result = null;
                    var    error  = e.Error;

                    if (error == null)
                    {
#if SILVERLIGHT
                        if (!string.IsNullOrEmpty(e.Result))
                        {
                            result = System.Text.Encoding.UTF8.GetBytes(e.Result);
                        }
#else
                        result = e.Result;
#endif
                    }
                    else if (error is WebException)
                    {
                        error = new WebExceptionWrapper((WebException)error);
                    }

                    DownloadDataCompleted(o, new DownloadDataCompletedEventArgsWrapper(error, e.Cancelled, e.UserState, result));
                }
            };
        }
Пример #6
0
        public WebClientWrapper(WebClient webClient)
        {
            _webClient = webClient;

#if SILVERLIGHT
            _webClient.UploadStringCompleted +=
#else
            _webClient.UploadDataCompleted +=
#endif
                (o, e) =>
                {
                    if (UploadDataCompleted == null)
                    {
                        return;
                    }

                    if (e == null)
                    {
                        UploadDataCompleted(o, null);
                    }
                    else
                    {
                        byte[] result = null;
                        var error = e.Error;

                        if (error == null)
                        {
#if SILVERLIGHT
                            if(!string.IsNullOrEmpty(e.Result))
                            {
                                result = System.Text.Encoding.UTF8.GetBytes(e.Result);
                            }
#else
                            result = e.Result;
#endif
                        }
                        else if (error is WebException)
                        {
                            error = new WebExceptionWrapper((WebException)error);
                        }

                        UploadDataCompleted(o, new UploadDataCompletedEventArgsWrapper(error, e.Cancelled, e.UserState, result));
                    }
                };

#if SILVERLIGHT
            _webClient.DownloadStringCompleted +=
#else
            _webClient.DownloadDataCompleted +=
#endif

                (o, e) =>
                {
                    if (DownloadDataCompleted == null)
                    {
                        return;
                    }

                    if (e == null)
                    {
                        DownloadDataCompleted(o, null);
                    }
                    else
                    {
                        byte[] result = null;
                        var error = e.Error;

                        if (error == null)
                        {
#if SILVERLIGHT
                            if(!string.IsNullOrEmpty(e.Result))
                            {
                                result = System.Text.Encoding.UTF8.GetBytes(e.Result);
                            }
#else
                            result = e.Result;
#endif
                        }
                        else if (error is WebException)
                        {
                            error = new WebExceptionWrapper((WebException)error);
                        }

                        DownloadDataCompleted(o, new DownloadDataCompletedEventArgsWrapper(error, e.Cancelled, e.UserState, result));
                    }
                };
        }