private static void HandleConnection(WebException ex)
        {
            Log.Error(ex);
            StatusNotification.Notify("Connection failed");

            if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            {
                MessageBox.Show("Could not find the server. Please check the URL.", "Connection failed", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else if ( ex.Response is HttpWebResponse)
            {
                var response = (HttpWebResponse)ex.Response;

                switch (response.StatusCode)
                {
                    case HttpStatusCode.BadGateway:
                        MessageBox.Show("Could not find the server. Please check the URL.", "Connection failed", MessageBoxButton.OK, MessageBoxImage.Information);
                        break;
                    case HttpStatusCode.Unauthorized:
                    case HttpStatusCode.Forbidden:
                        MessageBox.Show("You are not authorized to open this site", "Connection failed", MessageBoxButton.OK, MessageBoxImage.Information);
                        break;
                    default:
                        MessageBox.Show(ex.Message, "Connection failed", MessageBoxButton.OK, MessageBoxImage.Information);
                        break;
                }
            }
            else
                MessageBox.Show(ex.ToString(), "Connection failed", MessageBoxButton.OK, MessageBoxImage.Information);
        }
 public static void Log(WebException webException, ref string strErrors)
 {
     string strContext = webException.ToString();
     var errorStream = webException.Response.GetResponseStream();
     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ODataError));
     ODataError getError = (ODataError)(ser.ReadObject(errorStream));
     string strCode = getError.error.code;
     string strDetail = getError.error.message.value;
     strErrors += strDetail;
     GraphHelperEventSource.Log.WebFailure(strContext, strCode, strDetail);
 }
Esempio n. 3
0
 public RestResponse(WebException ex)
 {
     this.ErrorStatus = ex.Status;
     this.CompleteErrorMessage = ex.ToString();
     using (HttpWebResponse resp = (HttpWebResponse)ex.Response)
     {
         if (resp != null)//is null if no connection or timeout etc.
         {
             this.StatusCode = resp.StatusCode;
             this.Content = GetContent(resp);
         }
     }
 }
Esempio n. 4
0
 private static void ProcessWebException(WebException e)
 {
     Console.WriteLine("{0}", e.ToString());
     string strResponse = string.Empty;
     using (HttpWebResponse response = (HttpWebResponse)e.Response)
     {
         using (Stream responseStream = response.GetResponseStream())
         {
             using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
             {
                 strResponse = sr.ReadToEnd();
             }
         }
     }
     Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);
 }
Esempio n. 5
0
        private string GetErrorMessage(WebException e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(e.ToString());

            // Obtain detailed error information
            string strResponse = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)e.Response)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                    {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }
            sb.AppendLine("Http status code=" + e.Status + ", error message=" + strResponse);

            return sb.ToString();
        }
Esempio n. 6
0
        /// <summary>
        /// GetErrorMessage
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private string GetErrorMessage(System.Net.WebException e)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(e.ToString());

            // Obtain detailed error information
            string strResponse = string.Empty;

            using (HttpWebResponse response = ( HttpWebResponse )e.Response)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                    {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }
            sb.AppendLine("Http status code=" + e.Status + ", error message=" + strResponse);

            return(sb.ToString());
        }
 private void Log(WebException ex)
 {
     var logger = LogManager.GetLogger(typeof(TableStorageRequest));
     if(logger != null) logger.Error(ex.ToString());
 }
Esempio n. 8
0
 /// <summary>
 /// Internals the handle.
 /// </summary>
 /// <param name="ex">The executable.</param>
 /// <returns></returns>
 private ErrorAction InternalHandle(WebException ex)
 {
     return new ErrorAction(ErrorActionType.Retry, -2147204346, ex.ToString());
 }
Esempio n. 9
0
 private string ExceptionToString(WebException webException)
 {
     //ToDo : Build web exception
     return webException.ToString();
 }
        private string ProcessWebException(WebException e, string message)
        {
            Console.WriteLine("{0}: {1}", message, e.ToString());

            // Obtain detailed error information
            string strResponse = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)e.Response)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                    {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }
            return String.Format("Http status code={0}, error message={1}", e.Status, strResponse);
        }
        private void HandleWebException(WebException we, HttpState httpState)
        {
            HttpStatusCode statusCode = GetErrorCode(we.Response as HttpWebResponse);

            // It hasn't an active INTERNET connection (resolve error),
            // or the connection was LOST after http request was sent (connect failure)
            // It disconnects automatically and stops. So you don't need to call Disconnect().
            if (we.Status == WebExceptionStatus.NameResolutionFailure ||
                we.Status == WebExceptionStatus.ProxyNameResolutionFailure ||
                we.Status == WebExceptionStatus.ConnectFailure ||
                we.Status == WebExceptionStatus.ProtocolError)
            {
                isWebRequestInProcess = false;
                Disconnect();

                OnConnectingException(we);
                return;
            }

            // If the session corrupted (BadRequest),
            // or the first http request (Open) is timed out then reconnect: We passed resolve/connect failures.
            // IMPORTANT: Don't send httpState.OutgoingData again...
            if (statusCode == HttpStatusCode.BadRequest ||
                (we.Status == WebExceptionStatus.Timeout && httpState.PollAction == HttpPollAction.Open))
            {
                isWebRequestInProcess = false;
                Disconnect();
                Connect();
                return;
            }

            switch (we.Status)
            {
                // If a soft error occurs, re-send the last packet.
                case WebExceptionStatus.ConnectionClosed:
                case WebExceptionStatus.PipelineFailure:
                case WebExceptionStatus.KeepAliveFailure:
                case WebExceptionStatus.Timeout:
                case WebExceptionStatus.SendFailure:
                case WebExceptionStatus.ReceiveFailure:
                    {
                        isWebRequestInProcess = false;

                        // IN MIDDLE SOFT ERROR
                        // Outgoing data wasn't sent, we can re-send...
                        if (httpState.OutgoingData != null && httpState.OutgoingData.Length > 0)
                        {
                            Send(httpState.OutgoingData, httpState.UserState);
                        }
                        else
                        {
                            // AT END SOFT ERROR
                            // Outgoing data was sent, but an error occured while closing (KeepAliveFailure)
                            // Anyway this is not fatal, next time we will send PNG...
                            return;
                        }
                    }
                    break;

                case WebExceptionStatus.SecureChannelFailure:
                    {
                        OnConnectionException(we);
                        goto default;
                    }

                case WebExceptionStatus.RequestCanceled:
                case WebExceptionStatus.UnknownError:
                default:
                    {
                        OnDisconnected();

                        Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                            "HTTP Error: " + we.ToString(), GetType().Name);

                        break;
                    }
            }
        }
Esempio n. 12
0
        private void ProcessWebException(WebException e, string message)
        {
            Debug.WriteLine("{0}: {1}", message, e.ToString());

            // Obtain detailed error information
            string strResponse = string.Empty;

            using (HttpWebResponse response = (HttpWebResponse)e.Response) {
                using (Stream responseStream = response.GetResponseStream()) {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.UTF8)) {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }

            Debug.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);

            if (Failure != null) {
                Failure(strResponse);
            }
        }
 public static void LogException(WebException ex)
 {
     Trace.WriteLine("== BEGIN WebException =====================");
     Trace.WriteLine("Status: " + ex.Status);
     Trace.WriteLine(ex.ToString());
     HttpWebResponse response = ex.Response as HttpWebResponse;
     if (response != null)
         Trace.WriteLine(DumpResponse(response));
     Trace.WriteLine("== END WebException =======================");
 }
Esempio n. 14
0
 public void LogThisError(WebException e)
 {
     StringBuilder builder = new StringBuilder();
     builder.Append("Exception occured");
     builder.Append(string.Format("Exception Details : {0}", e.ToString()));
     LogThis(builder.ToString(), eloglevel.error);
 }
Esempio n. 15
0
        private void HandleWebException(WebException ex)
        {
            var sb = new StringBuilder();
            sb.AppendLine("-------------Error-------------");

            sb.AppendLine(ex.ToString());

            sb.AppendLine("");


            using (var response = ex.Response)
            {
                try
                {
                    var httpResponse = (HttpWebResponse)response;
                    sb.AppendLine("-------------Message-------------");
                    sb.AppendLine(string.Format("StatusCode:{0}", httpResponse.StatusCode));
                    sb.AppendLine(string.Format("Status:{0}", httpResponse.StatusDescription));
                    using (var data = response.GetResponseStream())
                    {
                        var text = new StreamReader(data).ReadToEnd();
                        sb.AppendLine(text);
                    }
                }
                catch
                {
                }
            }
            sb.AppendLine("");

            outputTextBox.Text = sb.ToString();
        }
 internal static HttpException FromWebException(WebException webException) {
   try {
     GoogleEmailUploaderTrace.EnteringMethod(
         "HttpException.FromWebException");
     GoogleEmailUploaderTrace.WriteLine(
         "Exception ({0}): {1}",
         webException.Status.ToString(),
         webException.ToString());
     HttpResponse httpResponse = null;
     if (webException.Response != null) {
       GoogleEmailUploaderTrace.WriteLine(
           "Headers: {0}",
           webException.Response.Headers.ToString());
       httpResponse = new HttpResponse((HttpWebResponse)webException.Response);
     }
     HttpExceptionStatus httpExceptionStatus;
     switch (webException.Status) {
       case WebExceptionStatus.ProtocolError:
         if (webException.Response != null) {
           HttpStatusCode httpStatusCode =
               ((HttpWebResponse)webException.Response).StatusCode;
           if (httpStatusCode == HttpStatusCode.Unauthorized) {
             httpExceptionStatus = HttpExceptionStatus.Unauthorized;
             break;
           } else if (httpStatusCode == HttpStatusCode.Forbidden) {
             httpExceptionStatus = HttpExceptionStatus.Forbidden;
             break;
           } else if (httpStatusCode == HttpStatusCode.BadGateway) {
             httpExceptionStatus = HttpExceptionStatus.BadGateway;
             break;
           } else if (httpStatusCode == HttpStatusCode.Conflict) {
             httpExceptionStatus = HttpExceptionStatus.Conflict;
             break;
           } else if (httpStatusCode == HttpStatusCode.BadRequest) {
             httpExceptionStatus = HttpExceptionStatus.BadRequest;
             break;
           }
         }
         httpExceptionStatus = HttpExceptionStatus.ProtocolError;
         break;
       case WebExceptionStatus.Timeout:
         httpExceptionStatus = HttpExceptionStatus.Timeout;
         break;
       default:
         httpExceptionStatus = HttpExceptionStatus.Other;
         break;
     }
     return new HttpException(webException.Message,
                              httpExceptionStatus,
                              httpResponse);
   } finally {
     GoogleEmailUploaderTrace.ExitingMethod(
         "HttpException.FromWebException");
   }
 }