CreateCustomException() 정적인 개인적인 메소드

static private CreateCustomException ( string uri, AuthenticationException ex ) : AuthenticationException
uri string
ex AuthenticationException
리턴 AuthenticationException
예제 #1
0
        private void HandleResponseError <TResponse>(Exception exception, AsyncState <TResponse> state)
        {
            var webEx = exception as WebException;

            if (webEx.IsWebException())
            {
                var errorResponse = ((HttpWebResponse)webEx.Response);
                Log.Error(webEx);
                Log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                Log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode = (int)errorResponse.StatusCode,
                };

                try
                {
                    using (var stream = errorResponse.GetResponseStream())
                    {
                        //Uncomment to Debug exceptions:
                        //var strResponse = new StreamReader(stream).ReadToEnd();
                        //Console.WriteLine("Response: " + strResponse);
                        //stream.Position = 0;
                        serviceEx.ResponseBody = stream.ReadFully().FromUtf8Bytes();

                        stream.ResetStream();

                        serviceEx.ResponseDto = this.StreamDeserializer(typeof(TResponse), stream);
                        state.HandleError((TResponse)serviceEx.ResponseDto, serviceEx);
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    Log.Debug(string.Format("WebException Reading Response Error: {0}", innerEx.Message), innerEx);
                    state.HandleError(default(TResponse), new WebServiceException(errorResponse.StatusDescription, innerEx)
                    {
                        StatusCode = (int)errorResponse.StatusCode,
                    });
                }
                return;
            }

            var authEx = exception as AuthenticationException;

            if (authEx != null)
            {
                var customEx = WebRequestUtils.CreateCustomException(state.Url, authEx);

                Log.Debug(string.Format("AuthenticationException: {0}", customEx.Message), customEx);
                state.HandleError(default(TResponse), authEx);
            }

            Log.Debug(string.Format("Exception Reading Response Error: {0}", exception.Message), exception);
            state.HandleError(default(TResponse), exception);

            CancelAsyncFn = null;
        }
예제 #2
0
        public void ThrowWebServiceException <TResponse>(Exception ex, string requestUri)
        {
            var webEx = ex as WebException;

            if (webEx != null && webEx.Response != null
#if !(SL5 || PCL)
                && webEx.Status == WebExceptionStatus.ProtocolError
#endif
                )
            {
                var errorResponse = ((HttpWebResponse)webEx.Response);
                log.Error(webEx);
                log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode        = (int)errorResponse.StatusCode,
                    StatusDescription = errorResponse.StatusDescription,
                };

                try
                {
                    if (errorResponse.ContentType.MatchesContentType(ContentType))
                    {
                        var bytes = errorResponse.GetResponseStream().ReadFully();
                        using (__requestAccess())
                            using (var stream = new MemoryStream(bytes))
                            {
                                serviceEx.ResponseBody = bytes.FromUtf8Bytes();
                                serviceEx.ResponseDto  = DeserializeFromStream <TResponse>(stream);
                            }
                    }
                    else
                    {
                        serviceEx.ResponseBody = errorResponse.GetResponseStream().ToUtf8String();
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    throw new WebServiceException(errorResponse.StatusDescription, innerEx)
                          {
                              StatusCode        = (int)errorResponse.StatusCode,
                              StatusDescription = errorResponse.StatusDescription,
                              ResponseBody      = serviceEx.ResponseBody
                          };
                }

                //Escape deserialize exception handling and throw here
                throw serviceEx;
            }

            var authEx = ex as AuthenticationException;
            if (authEx != null)
            {
                throw WebRequestUtils.CreateCustomException(requestUri, authEx);
            }
        }
예제 #3
0
        private void SendRequest(string httpMethod, string requestUri, object request, Action <WebRequest> callback)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException("httpMethod");
            }

            var client = (HttpWebRequest)WebRequest.Create(requestUri);

            try
            {
                client.Accept = ContentType;
                client.Method = httpMethod;

                if (this.credentials != null)
                {
                    client.Credentials = this.credentials;
                }
                if (this.AlwaysSendBasicAuthHeader)
                {
                    client.AddBasicAuth(this.UserName, this.Password);
                }

                if (StoreCookies)
                {
                    client.CookieContainer = CookieContainer;
                }

                if (this.RequestFilter != null)
                {
                    RequestFilter(client);
                }

                if (GlobalRequestFilter != null)
                {
                    GlobalRequestFilter(client);
                }

                if (httpMethod != HttpMethods.Get &&
                    httpMethod != HttpMethods.Delete)
                {
                    client.ContentType = ContentType;

                    client.BeginGetRequestStream(delegate(IAsyncResult target)
                    {
                        var webReq        = (HttpWebRequest)target.AsyncState;
                        var requestStream = webReq.EndGetRequestStream(target);
                        SerializeToStream(null, request, requestStream);
                        callback(client);
                    }, null);
                }
            }
            catch (AuthenticationException ex)
            {
                throw WebRequestUtils.CreateCustomException(requestUri, ex) ?? ex;
            }
        }
예제 #4
0
        private void HandleResponseError <TResponse>(Exception exception, AsyncState <TResponse> state)
        {
            var webEx = exception as WebException;

            if (PclExportClient.Instance.IsWebException(webEx))
            {
                var errorResponse = (HttpWebResponse)webEx.Response;
                Log.Error(webEx);
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                    Log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);
                }

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode        = (int)errorResponse.StatusCode,
                    StatusDescription = errorResponse.StatusDescription,
                    ResponseHeaders   = errorResponse.Headers
                };

                try
                {
                    using (var stream = errorResponse.GetResponseStream())
                    {
                        var bytes = stream.ReadFully();
                        serviceEx.ResponseBody = bytes.FromUtf8Bytes();
                        var errorResponseType = WebRequestUtils.GetErrorResponseDtoType <TResponse>(state.Request);

                        if (stream.CanSeek)
                        {
                            PclExport.Instance.ResetStream(stream);
                            serviceEx.ResponseDto = this.StreamDeserializer(errorResponseType, stream);
                        }
                        else //Android
                        {
                            using (var ms = MemoryStreamFactory.GetStream(bytes))
                            {
                                serviceEx.ResponseDto = this.StreamDeserializer(errorResponseType, ms);
                            }
                        }
                        state.HandleError(serviceEx.ResponseDto, serviceEx);
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    Log.Debug(string.Format("WebException Reading Response Error: {0}", innerEx.Message), innerEx);
                    state.HandleError(default(TResponse), new WebServiceException(errorResponse.StatusDescription, innerEx)
                    {
                        StatusCode        = (int)errorResponse.StatusCode,
                        StatusDescription = errorResponse.StatusDescription,
                        ResponseHeaders   = errorResponse.Headers
                    });
                }
                return;
            }

            var authEx = exception as AuthenticationException;

            if (authEx != null)
            {
                var customEx = WebRequestUtils.CreateCustomException(state.Url, authEx);

                Log.Debug(string.Format("AuthenticationException: {0}", customEx.Message), customEx);
                state.HandleError(default(TResponse), authEx);
            }

            Log.Debug(string.Format("Exception Reading Response Error: {0}", exception.Message), exception);
            state.HandleError(default(TResponse), exception);

            CancelAsyncFn = null;
        }
예제 #5
0
        private WebRequest PrepareWebRequest(string httpMethod, string requestUri, object request, Action <HttpWebRequest> sendRequestAction)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException("httpMethod");
            }

            var httpMethodGetOrHead = httpMethod == HttpMethods.Get || httpMethod == HttpMethods.Head;

            if (httpMethodGetOrHead && request != null)
            {
                var queryString = QueryStringSerializer.SerializeToString(request);
                if (!string.IsNullOrEmpty(queryString))
                {
                    requestUri += "?" + queryString;
                }
            }

            var client = (HttpWebRequest)WebRequest.Create(requestUri);

            try
            {
                client.MaximumResponseHeadersLength = int.MaxValue; //throws "The message length limit was exceeded" exception
                client.Accept = Accept;
                client.Method = httpMethod;
                client.Headers.Add(Headers);

                if (Proxy != null)
                {
                    client.Proxy = Proxy;
                }
                if (this.Timeout.HasValue)
                {
                    client.Timeout = (int)this.Timeout.Value.TotalMilliseconds;
                }
                if (this.ReadWriteTimeout.HasValue)
                {
                    client.ReadWriteTimeout = (int)this.ReadWriteTimeout.Value.TotalMilliseconds;
                }
                if (this.credentials != null)
                {
                    client.Credentials = this.credentials;
                }

                if (null != this.authInfo)
                {
                    client.AddAuthInfo(this.UserName, this.Password, authInfo);
                }
                else
                {
                    if (this.AlwaysSendBasicAuthHeader)
                    {
                        client.AddBasicAuth(this.UserName, this.Password);
                    }
                }

                if (!DisableAutoCompression)
                {
                    client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                    client.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }

                if (StoreCookies)
                {
                    client.CookieContainer = CookieContainer;
                }

                client.AllowAutoRedirect = AllowAutoRedirect;

                ApplyWebRequestFilters(client);

                if (httpMethod != HttpMethods.Get &&
                    httpMethod != HttpMethods.Delete &&
                    httpMethod != HttpMethods.Head)
                {
                    client.ContentType = ContentType;

                    if (sendRequestAction != null)
                    {
                        sendRequestAction(client);
                    }
                }
            }
            catch (AuthenticationException ex)
            {
                throw WebRequestUtils.CreateCustomException(requestUri, ex) ?? ex;
            }
            return(client);
        }
예제 #6
0
        private Exception HandleResponseError <TResponse>(Exception exception, string url, object request)
        {
            var webEx = exception as WebException;

            if (PclExportClient.Instance.IsWebException(webEx))
            {
                var errorResponse = (HttpWebResponse)webEx.Response;
                Log.Error(webEx);
                if (Log.IsDebugEnabled)
                {
                    Log.Debug($"Status Code : {errorResponse.StatusCode}");
                    Log.Debug($"Status Description : {errorResponse.StatusDescription}");
                }

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode        = (int)errorResponse.StatusCode,
                    StatusDescription = errorResponse.StatusDescription,
                    ResponseHeaders   = errorResponse.Headers
                };

                try
                {
                    using var stream = errorResponse.ResponseStream();
                    var bytes = stream.ReadFully();
                    serviceEx.ResponseBody = bytes.FromUtf8Bytes();
                    var errorResponseType = WebRequestUtils.GetErrorResponseDtoType <TResponse>(request);

                    if (stream.CanSeek)
                    {
                        PclExport.Instance.ResetStream(stream);
                        serviceEx.ResponseDto = this.StreamDeserializer(errorResponseType, stream);
                    }
                    else //Android
                    {
                        using var ms          = MemoryStreamFactory.GetStream(bytes);
                        serviceEx.ResponseDto = this.StreamDeserializer(errorResponseType, ms);
                    }
                    return(serviceEx);
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    Log.Debug($"WebException Reading Response Error: {innerEx.Message}", innerEx);
                    return(new WebServiceException(errorResponse.StatusDescription, innerEx)
                    {
                        StatusCode = (int)errorResponse.StatusCode,
                        StatusDescription = errorResponse.StatusDescription,
                        ResponseHeaders = errorResponse.Headers
                    });
                }
            }

            if (exception is AuthenticationException authEx)
            {
                var customEx = WebRequestUtils.CreateCustomException(url, authEx);

                Log.Debug($"AuthenticationException: {customEx.Message}", customEx);
                return(authEx);
            }

            Log.Debug($"Exception Reading Response Error: {exception.Message}", exception);
            return(exception);
        }
예제 #7
0
        private WebRequest PrepareWebRequest(string httpMethod, string requestUri, object request, Action <HttpWebRequest> sendRequestAction)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException("httpMethod");
            }

            var httpMethodGetOrHead = httpMethod == HttpMethods.Get || httpMethod == HttpMethods.Head;

            if (httpMethodGetOrHead && request != null)
            {
                var queryString = QueryStringSerializer.SerializeToString(request);
                if (!string.IsNullOrEmpty(queryString))
                {
                    requestUri += "?" + queryString;
                }
            }

            var client = (HttpWebRequest)WebRequest.Create(requestUri);

            try
            {
                client.Accept = Accept;
                client.Method = httpMethod;
                PclExportClient.Instance.AddHeader(client, Headers);

#if !SL5
                if (Proxy != null)
                {
                    client.Proxy = Proxy;
                }
#endif
                PclExport.Instance.Config(client,
                                          allowAutoRedirect: AllowAutoRedirect,
                                          timeout: this.Timeout,
                                          readWriteTimeout: ReadWriteTimeout,
                                          userAgent: UserAgent);

                if (this.credentials != null)
                {
                    client.Credentials = this.credentials;
                }

                if (null != this.authInfo)
                {
                    client.AddAuthInfo(this.UserName, this.Password, authInfo);
                }
                else
                {
                    if (this.AlwaysSendBasicAuthHeader)
                    {
                        client.AddBasicAuth(this.UserName, this.Password);
                    }
                }

                if (!DisableAutoCompression)
                {
                    PclExport.Instance.AddCompression(client);
                }

                if (StoreCookies)
                {
                    PclExportClient.Instance.SetCookieContainer(client, this);
                }

                ApplyWebRequestFilters(client);

                if (httpMethod != HttpMethods.Get &&
                    httpMethod != HttpMethods.Delete &&
                    httpMethod != HttpMethods.Head)
                {
                    client.ContentType = ContentType;

                    if (sendRequestAction != null)
                    {
                        sendRequestAction(client);
                    }
                }
            }
            catch (AuthenticationException ex)
            {
                throw WebRequestUtils.CreateCustomException(requestUri, ex) ?? ex;
            }
            return(client);
        }