예제 #1
0
 private void InitOption(HttpStatus status, HttpCause cause)
 {
     this.mStatus = status;
     this.mCause = cause;
     this.mHttpErrorResponse = null;
     this.mWebStatus = WebExceptionStatus.Success;
 }
 internal WebException(string message, string data, Exception innerException, WebExceptionStatus status, WebResponse response, WebExceptionInternalStatus internalStatus) : base(message + ((data != null) ? (": '" + data + "'") : ""), innerException)
 {
     this.m_Status = WebExceptionStatus.UnknownError;
     this.m_Status = status;
     this.m_Response = response;
     this.m_InternalStatus = internalStatus;
 }
예제 #3
0
	public WebException(String msg, Exception inner, 
		WebExceptionStatus status, WebResponse response) 
		: base(msg, inner) 
			{
				myresponse = response;
				mystatus = status;
			}
 /// <summary>
 /// Represents errors that occur during application execution
 /// </summary>
 /// <param name="message">The message that describes the error</param>
 /// <param name="response">The response from server</param>
 /// <param name="status">The <see cref="System.Net.WebExceptionStatus"/> that triggered this exception.</param>
 /// <param name="request">HTTP request sent by this SDK.</param>
 public ConnectionException(string message, string response, WebExceptionStatus status, HttpWebRequest request)
     : base(message)
 {
     this.Response = response;
     this.WebExceptionStatus = status;
     this.Request = request;
 }
 internal override IAsyncResult BeginMultipleWrite(BufferOffsetSize[] buffers, AsyncCallback callback, object state)
 {
     IAsyncResult result2;
     if (!this.m_Worker.IsAuthenticated)
     {
         BufferAsyncResult result = new BufferAsyncResult(this, buffers, state, callback);
         if (this.ProcessAuthentication(result))
         {
             return result;
         }
     }
     try
     {
         result2 = this.m_Worker.SecureStream.BeginWrite(buffers, callback, state);
     }
     catch
     {
         if (this.m_Worker.IsCertValidationFailed)
         {
             this.m_ExceptionStatus = WebExceptionStatus.TrustFailure;
         }
         else if (this.m_Worker.LastSecurityStatus != SecurityStatus.OK)
         {
             this.m_ExceptionStatus = WebExceptionStatus.SecureChannelFailure;
         }
         else
         {
             this.m_ExceptionStatus = WebExceptionStatus.SendFailure;
         }
         throw;
     }
     return result2;
 }
예제 #6
0
        //
        // This version of an Ssl Stream is for internal HttpWebrequest use.
        // This Ssl client owns the underlined socket
        // The TlsStream will own secured read/write and disposal of the passed "networkStream" stream.
        //
        public TlsStream(string destinationHost, NetworkStream networkStream, X509CertificateCollection clientCertificates, ServicePoint servicePoint, object initiatingRequest, ExecutionContext executionContext)
               :base(networkStream, true) {

        // WebRequest manages the execution context manually so we have to ensure we get one for SSL client certificate demand
        _ExecutionContext = executionContext;
        if (_ExecutionContext == null)
        {
            _ExecutionContext = ExecutionContext.Capture();
        }

        // 


         GlobalLog.Enter("TlsStream::TlsStream", "host="+destinationHost+", #certs="+((clientCertificates == null) ? "none" : clientCertificates.Count.ToString(NumberFormatInfo.InvariantInfo)));
         if (Logging.On) Logging.PrintInfo(Logging.Web, this, ".ctor", "host="+destinationHost+", #certs="+((clientCertificates == null) ? "null" : clientCertificates.Count.ToString(NumberFormatInfo.InvariantInfo)));

         m_ExceptionStatus = WebExceptionStatus.SecureChannelFailure;
         m_Worker = new SslState(networkStream, initiatingRequest is HttpWebRequest, SettingsSectionInternal.Section.EncryptionPolicy);

         m_DestinationHost = destinationHost;
         m_ClientCertificates = clientCertificates;

         RemoteCertValidationCallback certValidationCallback = servicePoint.SetupHandshakeDoneProcedure(this, initiatingRequest);
         m_Worker.SetCertValidationDelegate(certValidationCallback);

         // The Handshake is NOT done at this point
         GlobalLog.Leave("TlsStream::TlsStream (Handshake is not done)");
        }
예제 #7
0
	public WebException(String msg, WebExceptionStatus status)
		: base(msg)
			{
				myresponse = null;
				mystatus = status;
			#if !ECMA_COMPAT
				HResult = unchecked((int)0x80131509);
			#endif
			}
예제 #8
0
		public WebException(string message, 
				    Exception innerException,
		   		    WebExceptionStatus status, 
		   		    WebResponse response)
			: base (message, innerException)
		{
			this.status = status;
			this.response = response;
		}
예제 #9
0
        static HttpWebException()
        {
#if PORTABLE
            if (!Enum.TryParse("ProtocolError", out ProtocolError))
                ProtocolError = WebExceptionStatus.UnknownError;
#else
            ProtocolError = WebExceptionStatus.ProtocolError;
#endif
        }
예제 #10
0
 public HttpExceptionEx(HttpStatus status, HttpCause cause, object baseException)
 {
     this.InitOption(status, cause);
     if (baseException.GetType() == typeof(WebException))
     {
         this.mException = (WebException) baseException;
         this.mWebStatus = ((WebException) baseException).Status;
         this.mHttpErrorResponse = (HttpWebResponse) ((WebException) baseException).Response;
     }
     else
     {
         this.mException = (Exception) baseException;
     }
 }
예제 #11
0
        public WebException(string message,
                            Exception innerException,
                            WebExceptionStatus status,
                            WebResponse response) :
            base(message, innerException)
        {
            _status = status;
            _response = response;

            if (innerException != null)
            {
                HResult = innerException.HResult;
            }
        }
예제 #12
0
        public WebException(string message,
                            Exception innerException,
                            WebExceptionStatus status,
                            WebResponse response) :
            base(message, innerException)
        {
            _status   = status;
            _response = response;

            if (innerException != null)
            {
                HResult = innerException.HResult;
            }
        }
예제 #13
0
        public static Resposta Get(string url)
        {
            Resposta resposta = new Resposta();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Get;
                request.Accept = "application/json";

                request.Timeout = 2400000;

                HttpWebResponse response           = (HttpWebResponse)request.GetResponse();
                Stream          dataStream         = response.GetResponseStream();
                StreamReader    reader             = new StreamReader(dataStream);
                string          responseFromServer = reader.ReadToEnd();

                resposta = JsonConvert.DeserializeObject <Resposta>(responseFromServer);
                resposta.httpStatusCode = (int)response.StatusCode;

                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (WebException ex)
            {
                WebExceptionStatus status = ex.Status;

                using (var stream = ex.Response.GetResponseStream())
                    using (var _reader = new StreamReader(stream))
                    {
                        var r = _reader.ReadToEnd();
                        resposta = JsonConvert.DeserializeObject <Resposta>(r);
                    }

                if (status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
                    resposta.httpStatusCode = (int)httpResponse.StatusCode;
                }
            }
            catch (Exception e)
            {
                resposta.httpStatusCode = 500;
                resposta.message        = e.InnerException.ToString();
                return(resposta);
            }

            return(resposta);
        }
예제 #14
0
        bool CreateStream(HttpWebRequest request)
        {
            try {
                NetworkStream serverStream = new NetworkStream(socket, false);

                if (request.Address.Scheme == Uri.UriSchemeHttps)
                {
                    ssl = true;
                    EnsureSSLStreamAvailable();
                    if (!reused || nstream == null || nstream.GetType() != sslStream)
                    {
                        byte [] buffer = null;
                        if (sPoint.UseConnect)
                        {
                            bool ok = CreateTunnel(request, serverStream, out buffer);
                            if (!ok)
                            {
                                return(false);
                            }
                        }

                        object[] args = new object [4] {
                            serverStream,
                            request.ClientCertificates,
                            request, buffer
                        };
                        nstream        = (Stream)Activator.CreateInstance(sslStream, args);
                        certsAvailable = false;
                    }
                    // we also need to set ServicePoint.Certificate
                    // and ServicePoint.ClientCertificate but this can
                    // only be done later (after handshake - which is
                    // done only after a read operation).
                }
                else
                {
                    ssl     = false;
                    nstream = serverStream;
                }
            } catch (Exception) {
                if (!request.Aborted)
                {
                    status = WebExceptionStatus.ConnectFailure;
                }
                return(false);
            }

            return(true);
        }
        public static async Task PostRequestAsync(string url, string postData)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                request.Method = "POST";

                // данные для отправки
                string data = postData; // Example: "sName=Иван Иванов&age=31"

                // преобразуем данные в массив байтов
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);

                // устанавливаем тип содержимого - параметр ContentType
                request.ContentType = "application/x-www-form-urlencoded";

                // Устанавливаем заголовок Content-Length запроса - свойство ContentLength
                request.ContentLength = byteArray.Length;

                //записываем данные в поток запроса
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }

                WebResponse response = await request.GetResponseAsync();

                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        Console.WriteLine(reader.ReadToEnd());
                    }
                }
                response.Close();
            }
            catch (WebException e)
            {
                // получаем статус исключения
                WebExceptionStatus status = e.Status;

                if (status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)e.Response;
                    Console.WriteLine("Статусный код ошибки: {0} - {1}",
                                      (int)httpResponse.StatusCode, httpResponse.StatusCode);
                }
            }
        }
예제 #16
0
        bool CreateStream(HttpWebRequest request)
        {
            try {
                NetworkStream serverStream = new NetworkStream(socket, false);

                if (request.Address.Scheme == Uri.UriSchemeHttps)
                {
#if SECURITY_DEP
                    if (!reused || nstream == null || tlsStream == null)
                    {
                        byte [] buffer = null;
                        if (sPoint.UseConnect)
                        {
                            bool ok = CreateTunnel(request, sPoint.Address, serverStream, out buffer);
                            if (!ok)
                            {
                                return(false);
                            }
                        }
                        tlsStream = new MonoTlsStream(request, serverStream);
                        nstream   = tlsStream.CreateStream(buffer);
                    }
                    // we also need to set ServicePoint.Certificate
                    // and ServicePoint.ClientCertificate but this can
                    // only be done later (after handshake - which is
                    // done only after a read operation).
#else
                    throw new NotSupportedException();
#endif
                }
                else
                {
                    nstream = serverStream;
                }
            } catch (Exception ex) {
                if (tlsStream != null)
                {
                    status = tlsStream.ExceptionStatus;
                }
                else if (!request.Aborted)
                {
                    status = WebExceptionStatus.ConnectFailure;
                }
                connect_exception = ex;
                return(false);
            }

            return(true);
        }
예제 #17
0
        private string GetMessage(WebExceptionStatus code, string msg)
        {
            string message = "异常";

            IsSuccess = false;
            switch (code)
            {
            case WebExceptionStatus.Success:
                message   = "成功";
                IsSuccess = true;
                break;

            case WebExceptionStatus.NameResolutionFailure:
            case WebExceptionStatus.ConnectFailure:
                message = "请检测网络";     // 无法解析服务器名称
                break;

            case WebExceptionStatus.ReceiveFailure:
            case WebExceptionStatus.SendFailure:
            case WebExceptionStatus.PipelineFailure:
            case WebExceptionStatus.ProtocolError:
            case WebExceptionStatus.ConnectionClosed:
            case WebExceptionStatus.TrustFailure:
            case WebExceptionStatus.SecureChannelFailure:
            case WebExceptionStatus.ServerProtocolViolation:
            case WebExceptionStatus.KeepAliveFailure:
            case WebExceptionStatus.ProxyNameResolutionFailure:
            case WebExceptionStatus.UnknownError:
            case WebExceptionStatus.MessageLengthLimitExceeded:
            case WebExceptionStatus.CacheEntryNotFound:
            case WebExceptionStatus.RequestProhibitedByCachePolicy:
            case WebExceptionStatus.RequestProhibitedByProxy:
                message = msg;     //"发生未知类型的异常";
                break;

            case WebExceptionStatus.RequestCanceled:
                message = "请求被取消";
                break;

            case WebExceptionStatus.Timeout:
                message = "请求超时";
                break;

            case WebExceptionStatus.Pending:
                IsSuccess = true;
                break;
            }
            return(message);
        }
예제 #18
0
        public async Task <T> GenericGetAsync <T>(string path) where T : class
        {
            try
            {
                T result = null;
                if (_inMemoryCache.ContainsKey(path))
                {
                    Console.WriteLine($"Data from cache for: {path}");
                    result = _inMemoryCache[path] as T;
                    return(result);
                }

                HttpResponseMessage response = await client.GetAsync(path);

                if (response.IsSuccessStatusCode)
                {
                    result = await response.Content.ReadAsAsync <T>();

                    _inMemoryCache[path] = result;
                }
                else
                {
                    var errorMessage = $"Unsuccess Response for: {RootUri}{path}";
                    errorMessage += $"\nstatus code: {response.StatusCode.ToString()}";
                    throw (new Exception(errorMessage));
                }
                return(result);
            }
            catch (WebException webExcp)
            {
                var errorMessage = $"WebException has been caught  for: {RootUri}{path}";
                errorMessage += $"\nwebExcp: {webExcp.ToString()}";

                WebExceptionStatus status = webExcp.Status;
                if (status == WebExceptionStatus.ProtocolError)
                {
                    errorMessage += $"\nThe server returned protocol error ";
                    HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
                    errorMessage += $"\n{(int)httpResponse.StatusCode} - {httpResponse.StatusCode}";
                }
                throw (new Exception(errorMessage));
            }
            catch (Exception ex)
            {
                var errorMessage = $"An Exception has been caught for: {RootUri}{path}";
                var ex2          = new Exception(errorMessage, ex);
                throw ex2;
            }
        }
예제 #19
0
        private string GetAllData(string Url)
        {
            string result = "";

            try
            {
                // Create a request for the URL.
                WebRequest request = WebRequest.Create(Url);
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;

                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                var          data = ((HttpWebResponse)response).GetResponseStream();
                StreamReader sr   = new StreamReader(response.GetResponseStream());
                result = sr.ReadToEnd();
                sr.Close();
            }
            catch (WebException webExcp)
            {
                // If you reach this point, an exception has been caught.
                Console.WriteLine("A WebException has been caught.");
                // Write out the WebException message.
                Console.WriteLine(webExcp.ToString());
                //this.ErrorMsg("Error: A WebException has been caught.");
                // Get the WebException status code.
                WebExceptionStatus status = webExcp.Status;
                //System.Windows.Forms.MessageBox.Show(status + ". Please press refresh icon!", "Message");
                // If status is WebExceptionStatus.ProtocolError,
                //   there has been a protocol error and a WebResponse
                //   should exist. Display the protocol error.
                if (status == WebExceptionStatus.ProtocolError)
                {
                    Console.Write("The server returned protocol error ");
                    //this.ErrorMsg("Error: The server returned protocol error.");
                    //System.Windows.Forms.MessageBox.Show("The server returned protocol error. Please press refresh icon!", "Message");
                    // Get HttpWebResponse so that you can check the HTTP status code.
                    HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
                    Console.WriteLine((int)httpResponse.StatusCode + " - "
                                      + httpResponse.StatusCode);
                }
            }
            catch (Exception e)
            {
                // Code to catch other exceptions goes here.
            }
            return(result);
        }
예제 #20
0
        private WebHeaderCollection ReadHeaders(HttpWebRequest request, Stream stream, out byte[] retBuffer, out int status)
        {
            retBuffer = null;
            status    = 200;
            byte[]              array               = new byte[1024];
            MemoryStream        memoryStream        = new MemoryStream();
            bool                flag                = false;
            WebHeaderCollection webHeaderCollection = null;

            while (true)
            {
                int num = stream.Read(array, 0, 1024);
                if (num == 0)
                {
                    break;
                }
                memoryStream.Write(array, 0, num);
                int    start  = 0;
                string output = null;
                webHeaderCollection = new WebHeaderCollection();
                while (ReadLine(memoryStream.GetBuffer(), ref start, (int)memoryStream.Length, ref output))
                {
                    if (output == null)
                    {
                        if (memoryStream.Length - start > 0)
                        {
                            retBuffer = new byte[memoryStream.Length - start];
                            Buffer.BlockCopy(memoryStream.GetBuffer(), start, retBuffer, 0, retBuffer.Length);
                        }
                        return(webHeaderCollection);
                    }
                    if (flag)
                    {
                        webHeaderCollection.Add(output);
                        continue;
                    }
                    int num2 = output.IndexOf(' ');
                    if (num2 == -1)
                    {
                        HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
                        return(null);
                    }
                    status = (int)uint.Parse(output.Substring(num2 + 1, 3));
                    flag   = true;
                }
            }
            HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
            return(null);
        }
예제 #21
0
        private void logNetworkRequestClick(object sender, RoutedEventArgs e)
        {
            Random random = new Random();

            string[] methods   = new string[] { "GET", "POST", "HEAD", "PUT" };
            string   method    = methods[random.Next(0, methods.Length)];
            string   urlString = randomURLString();
            // latency in milliseconds
            long latency      = (long)Math.Floor(4000.0 * random.NextDouble());
            long bytesRead    = random.Next(0, 10000);
            long bytesSent    = random.Next(0, 10000);
            long responseCode = 200;

            if (random.Next(0, 5) == 0)
            {
                // Some common response other than 200 == OK .
                long[] responseCodes = new long[] { 301, 308, 400, 401, 402, 403, 404, 405, 408, 500, 502, 503 };
                responseCode = responseCodes[random.Next(0, responseCodes.Length)];
            }
            ;
            WebExceptionStatus exceptionStatus = WebExceptionStatus.Success;

            if (random.Next(0, 5) == 0)
            {
                // Simulate a network WebException was encountered.
                WebExceptionStatus[] exceptionStatuses = new WebExceptionStatus[] {
                    WebExceptionStatus.NameResolutionFailure,
                    WebExceptionStatus.ConnectFailure,
                    WebExceptionStatus.ReceiveFailure,
                    WebExceptionStatus.SendFailure,
                    WebExceptionStatus.ConnectionClosed,
                    WebExceptionStatus.TrustFailure,
                    WebExceptionStatus.KeepAliveFailure,
                    WebExceptionStatus.Timeout
                };
                exceptionStatus = exceptionStatuses[random.Next(0, exceptionStatuses.Length)];
                // We didn't receive a simulated response, after all.
                responseCode = 0;
            }
            ;
            Crittercism.LogNetworkRequest(
                method,
                urlString,
                latency,
                bytesRead,
                bytesSent,
                (HttpStatusCode)responseCode,
                WebExceptionStatus.Success);
        }
예제 #22
0
        private static void PrintStatusCode(WebExceptionStatus statusCode)
        {
            var color = Console.ForegroundColor;

            if (statusCode == WebExceptionStatus.Success)
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
            }
            Console.WriteLine(String.Format("Status code was: {0}", statusCode));
            Console.ForegroundColor = color;
        }
예제 #23
0
        public static WebExceptionStatus ExecuteAndDeserialize <T>(this HttpWebRequest request, out HttpStatusCode statusCode, out T responseObject)
        {
            WebExceptionStatus status = request.Execute(out HttpWebResponse response);

            if (response.TryGetStatusCode(out statusCode))
            {
                responseObject = JsonConvert.DeserializeObject <T>(response.GetReponseString());
            }
            else
            {
                responseObject = default;
            }

            return(status);
        }
예제 #24
0
 public EtcdResult Enqueue(string key, string value, long?ttl = null)
 {
     foreach (EtcdServer srv in serverList)
     {
         string uri = BuildRequestUri(srv.host, srv.port, "keys", key);
         NameValueCollection data       = BuildRequestData(null, value, ttl);
         string             responseStr = null;
         WebExceptionStatus status      = DoWebRequest(uri, "POST", data, out responseStr);
         if (status == WebExceptionStatus.Success || status == WebExceptionStatus.ProtocolError)
         {
             return(EtcdResult.Parse(responseStr));
         }
     }
     return(null);
 }
예제 #25
0
        public MonoTlsStream(HttpWebRequest request, NetworkStream networkStream)
        {
#if SECURITY_DEP
            this.request       = request;
            this.networkStream = networkStream;

            settings = request.TlsSettings;
            provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal();
            status   = WebExceptionStatus.SecureChannelFailure;

            ChainValidationHelper.Create(provider, ref settings, this);
#else
            throw new PlatformNotSupportedException(EXCEPTION_MESSAGE);
#endif
        }
 internal static string GetWebStatusString(WebExceptionStatus status)
 {
     int index = (int) status;
     if ((index >= s_Mapping.Length) || (index < 0))
     {
         throw new InternalException();
     }
     string str = s_Mapping[index];
     if (str == null)
     {
         str = "net_webstatus_" + status.ToString();
         s_Mapping[index] = str;
     }
     return str;
 }
예제 #27
0
        internal bool EndWrite(IAsyncResult result)
        {
            if (nstream == null)
            {
                return(false);
            }

            try {
                nstream.EndWrite(result);
                return(true);
            } catch {
                status = WebExceptionStatus.SendFailure;
                return(false);
            }
        }
예제 #28
0
 public HttpStatusCode GetHttpResponse()
 {
     try
     {
         response = client.GetResponse();
     }
     catch (WebException e)
     {
         status   = e.Status;
         response = e.Response;
         return(((HttpWebResponse)e.Response).StatusCode);
     }
     //Fix this status to enum
     return(HttpStatusCode.OK);
 }
예제 #29
0
        /*++

            GetWebStatusString - Get a WebExceptionStatus-specific resource string


            This method takes an input string and a WebExceptionStatus. We use the input
            string as a key to find a status message and the webStatus to produce
            a status-specific message, then we combine the two.

            Input:

                Res             - Id for resource string.
                Status          - The WebExceptionStatus to be formatted.

            Returns:

                string for localized message.

        --*/
        public static string GetWebStatusString(string Res, WebExceptionStatus Status) {
            string Msg;
            string StatusMsg;

            StatusMsg = SR.GetString(WebExceptionMapping.GetWebStatusString(Status));

            // Get the base status.

            Msg = SR.GetString(Res);

            // Format the status specific message into the base status and return
            // that

            return String.Format(CultureInfo.CurrentCulture, Msg, StatusMsg);
        }
예제 #30
0
        private bool IsConnectionAlive(WebExceptionStatus stastus)
        {
            switch (stastus)
            {
            case WebExceptionStatus.ConnectFailure:
            case WebExceptionStatus.ConnectionClosed:
            case WebExceptionStatus.ReceiveFailure:
            case WebExceptionStatus.SendFailure:
            case WebExceptionStatus.NameResolutionFailure:
                return(false);

            default:
                return(true);
            }
        }
예제 #31
0
        private static string GetErrorString(WebException webException)
        {
            WebExceptionStatus status = webException.Status;

            if (status == WebExceptionStatus.ProtocolError)
            {
                HttpWebResponse httpWebEx = (HttpWebResponse)webException.Response;
                string          result    = errors[(int)httpWebEx.StatusCode] != null ? errors[(int)httpWebEx.StatusCode].ToString() : "Неизвестная ошибка! Приносим свои извинения! Попробуйте, пожалуйста ещё раз.";
                return(result);
            }
            else
            {
                return("Проверьте подключение к интернету!");
            }
        }
예제 #32
0
        public HttpOperation(HttpServer server, string me, Handler handler, HttpOperationFlags flags,
                             HttpStatusCode expectedStatus, WebExceptionStatus expectedError)
        {
            Server         = server;
            Handler        = handler;
            Flags          = flags;
            ExpectedStatus = expectedStatus;
            ExpectedError  = expectedError;

            ME = $"[{GetType ().Name}({ID}:{me})]";

            requestTask     = Listener.TaskSupport.CreateAsyncCompletionSource <Request> ();
            requestDoneTask = Listener.TaskSupport.CreateAsyncCompletionSource <Response> ();
            cts             = new CancellationTokenSource();
        }
예제 #33
0
        public static string Http(string url, string data, string metod = "POST")
        {
            string result = null;

            try
            {
                WebRequest request = WebRequest.Create(url);
                request.Method = metod; // для отправки используется метод Post
                                        // данные для отправки
                                        // преобразуем данные в массив байтов
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);

                // устанавливаем тип содержимого - параметр ContentType
                //request.ContentType = "application/x-www-form-urlencoded";

                // Устанавливаем заголовок Content-Length запроса - свойство ContentLength
                request.ContentLength = byteArray.Length;

                //записываем данные в поток запроса
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }

                WebResponse response = request.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                response.Close();
            }
            catch (WebException ex)
            {
                // получаем статус исключения
                WebExceptionStatus status = ex.Status;

                if (status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
                    Console.WriteLine("Статусный код ошибки: {0} - {1}   ",
                                      (int)httpResponse.StatusCode, httpResponse.StatusCode);
                }
            }
            return(result);
        }
예제 #34
0
        public void StartRun()
        {
            WebExceptionStatus outS = WebExceptionStatus.UnknownError;

            foreach (var linkExtraction in this.configurationHotProduct.HotProduct_Link.Split(new char[] { ',', '\n', ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                this.LogData("Get html of cat page");
                string html = downloadHtml.GetHTML(linkExtraction, 45, 2, out outS, this.configurationHotProduct.HotProduct_UseSelenium);
                if (!string.IsNullOrEmpty(html))
                {
                    HtmlDocument htmlDocument = new HtmlDocument();
                    htmlDocument.LoadHtml(html);
                    var nodeLinks = htmlDocument.DocumentNode.SelectNodes(this.configurationHotProduct.HotProduct_Xpath);
                    if (nodeLinks != null)
                    {
                        foreach (var VARIABLE in nodeLinks)
                        {
                            try
                            {
                                string shortLink = VARIABLE.GetAttributeValue("href", "");

                                string fullLink = Common.GetAbsoluteUrl(shortLink, new Uri(this.company.Website));
                                this.LogData(string.Format("Process link product {0}", fullLink));
                                string htmlLinkProduct = this.downloadHtml.GetHTML(fullLink, 45, 2, out outS);

                                HtmlDocument h = new HtmlDocument();
                                h.LoadHtml(htmlLinkProduct);
                                if (!string.IsNullOrEmpty(htmlLinkProduct))
                                {
                                    ProductEntity productEntity = new ProductEntity();
                                    this.producerParser.Analytics(productEntity, h, fullLink, this.configuration, this.company.Domain);
                                    if (productEntity.IsSuccessData(true))
                                    {
                                        this.productAdapter.UpsertProductHot(productEntity);
                                        this.LogData(string.Format("Saved a product to database. {0} {1} {2}", productEntity.ID,
                                                                   productEntity.Name, productEntity.Price));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                LogData(string.Format("Error: {0} {1}", ex.Message, ex.StackTrace));
                            }
                        }
                    }
                }
            }
        }
예제 #35
0
        public static HttpWebHelper.ConnectivityError CheckConnectivityError(WebException e)
        {
            WebExceptionStatus status = e.Status;

            switch (status)
            {
            case WebExceptionStatus.NameResolutionFailure:
                return(HttpWebHelper.ConnectivityError.NonRetryable);

            case WebExceptionStatus.ConnectFailure:
            case WebExceptionStatus.SendFailure:
                break;

            case WebExceptionStatus.ReceiveFailure:
                goto IL_42;

            default:
                if (status != WebExceptionStatus.ConnectionClosed)
                {
                    switch (status)
                    {
                    case WebExceptionStatus.KeepAliveFailure:
                        break;

                    case WebExceptionStatus.Pending:
                        goto IL_42;

                    case WebExceptionStatus.Timeout:
                    case WebExceptionStatus.ProxyNameResolutionFailure:
                        return(HttpWebHelper.ConnectivityError.NonRetryable);

                    default:
                        goto IL_42;
                    }
                }
                break;
            }
            return(HttpWebHelper.ConnectivityError.Retryable);

IL_42:
            HttpWebResponse httpWebResponse = (HttpWebResponse)e.Response;

            if (httpWebResponse != null && (httpWebResponse.StatusCode == HttpStatusCode.ServiceUnavailable || httpWebResponse.StatusCode == HttpStatusCode.BadGateway || httpWebResponse.StatusCode == HttpStatusCode.GatewayTimeout))
            {
                return(HttpWebHelper.ConnectivityError.NonRetryable);
            }
            return(HttpWebHelper.ConnectivityError.None);
        }
예제 #36
0
        private bool CreateStream(HttpWebRequest request)
        {
            try
            {
                NetworkStream networkStream = new NetworkStream(socket, owns_socket: false);
                if (request.Address.Scheme == Uri.UriSchemeHttps)
                {
                    ssl = true;
                    EnsureSSLStreamAvailable();
                    if (!reused || nstream == null || nstream.GetType() != sslStream)
                    {
                        byte[] array = null;
                        if (sPoint.UseConnect && !CreateTunnel(request, networkStream, out array))
                        {
                            return(false);
                        }
                        object[] args = new object[4]
                        {
                            networkStream,
                            request.ClientCertificates,
                            request,
                            array
                        };
                        nstream = (Stream)Activator.CreateInstance(sslStream, args);
                        SslClientStream sslClientStream = (SslClientStream)nstream;
                        ServicePointManager.ChainValidationHelper @object = new ServicePointManager.ChainValidationHelper(request);
                        sslClientStream.ServerCertValidation2 += @object.ValidateChain;
                        certsAvailable = false;
                    }
                }
                else
                {
                    ssl     = false;
                    nstream = networkStream;
                }
            }
            catch (Exception)
            {
                if (!request.Aborted)
                {
                    status = WebExceptionStatus.ConnectFailure;
                }
                return(false);

                IL_011e :;
            }
            return(true);
        }
        /// <summary>
        /// Determines whether an error code is likely to have been caused by internet reachability problems.
        /// </summary>
        public static bool IsCannotReachInternetError(this WebExceptionStatus status)
        {
            switch (status)
            {
            case WebExceptionStatus.NameResolutionFailure:
            case WebExceptionStatus.ConnectFailure:
            case WebExceptionStatus.ConnectionClosed:
            case WebExceptionStatus.ProxyNameResolutionFailure:
            case WebExceptionStatus.SendFailure:
            case WebExceptionStatus.Timeout:
                return(true);

            default:
                return(false);
            }
        }
예제 #38
0
        /*++
         *
         *  GetWebStatusString - Get a WebExceptionStatus-specific resource string
         *
         *
         *  This method takes an input string and a WebExceptionStatus. We use the input
         *  string as a key to find a status message and the webStatus to produce
         *  a status-specific message, then we combine the two.
         *
         *  Input:
         *
         *      Res             - Id for resource string.
         *      Status          - The WebExceptionStatus to be formatted.
         *
         *  Returns:
         *
         *      string for localized message.
         *
         * --*/
        public static string GetWebStatusString(string Res, WebExceptionStatus Status)
        {
            string Msg;
            string StatusMsg;

            StatusMsg = SR.GetString(WebExceptionMapping.GetWebStatusString(Status));

            // Get the base status.

            Msg = SR.GetString(Res);

            // Format the status specific message into the base status and return
            // that

            return(String.Format(CultureInfo.CurrentCulture, Msg, StatusMsg));
        }
예제 #39
0
        public WebResponse GetHttpWebResponse()
        {
            try
            {
                response = client.GetResponse();
            }
            catch (WebException e)
            {
                status   = e.Status;
                response = e.Response;


                return(e.Response);
            }
            return(response);
        }
예제 #40
0
    public static void LogNetworkRequest(string method,
                                         string uriString,
                                         long latency,
                                         long bytesRead,
                                         long bytesSent,
                                         HttpStatusCode responseCode,
                                         WebExceptionStatus exceptionStatus)
    {
#if CRITTERCISM_ANDROID
        if (!isInitialized)
        {
            return;
        }
        PluginCallStatic("logNetworkRequest", method, uriString, latency, bytesRead, bytesSent, (int)responseCode, (int)exceptionStatus);
#endif
    }
 protected internal virtual void FailRequest(WebExceptionStatus webStatus)
 {
     if (Logging.On)
     {
         Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_failing_request_with_exception", new object[] { webStatus.ToString() }));
     }
     if (webStatus == WebExceptionStatus.CacheEntryNotFound)
     {
         throw ExceptionHelper.CacheEntryNotFoundException;
     }
     if (webStatus == WebExceptionStatus.RequestProhibitedByCachePolicy)
     {
         throw ExceptionHelper.RequestProhibitedByCachePolicyException;
     }
     throw new WebException(NetRes.GetWebStatusString("net_requestaborted", webStatus), webStatus);
 }
예제 #42
0
        public MonoTlsStream(HttpWebRequest request, NetworkStream networkStream)
        {
            this.request       = request;
            this.networkStream = networkStream;

            settings = request.TlsSettings;
            provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal();
            status   = WebExceptionStatus.SecureChannelFailure;

            if (settings == null)
            {
                settings = new MonoTlsSettings();
            }

            validationHelper = ChainValidationHelper.Create(ref settings, this);
        }
예제 #43
0
 static void OnFailed(string error, WebExceptionStatus status)
 {
     if (listenerCallBackFunctionID != -1)
     {
         Dictionary <string, object> dict = new Dictionary <string, object>();
         dict.Add("progress", 0);
         dict.Add("responseCode", 404);
         dict.Add("isReachable", false);
         if (luaState != 0)
         {
             LuaManager.GetLuaManager(luaState).ExecuteCallBackFunctionWithTableParam(listenerCallBackFunctionID, dict);
         }
         listenerCallBackFunctionID = -1;
         luaState = 0;
     }
 }
예제 #44
0
        public System.IO.Stream downloadURL(string url)
        {
            try
            {
                this.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
                return this.OpenRead(url);
            }
            catch(WebException e) {
                _lastWebError = e.Status;
#if DEBUG              
                Debug.Print("Download:{1}. Error: {0}.", e.Message, url);
#endif          
            }

            return null;
        }
예제 #45
0
 public string GetHTML(string url, int secondsTimeOut, int numTry, out WebExceptionStatus status, bool bUseSelenium = false)
 {
     if (!bUseSelenium)
     {
         return(downloadOld.GetHTML(url, secondsTimeOut, numTry, out status));
     }
     else
     {
         IWebDriver websDriver = new ChromeDriver();
         websDriver.Url = url;
         string str = websDriver.PageSource;
         websDriver.Close();
         status = WebExceptionStatus.UnknownError;
         return(str);
     }
 }
 public TlsStream(string destinationHost, NetworkStream networkStream, X509CertificateCollection clientCertificates, ServicePoint servicePoint, object initiatingRequest, ExecutionContext executionContext) : base(networkStream, true)
 {
     this.m_PendingIO = new ArrayList();
     this._ExecutionContext = executionContext;
     if (this._ExecutionContext == null)
     {
         this._ExecutionContext = ExecutionContext.Capture();
     }
     if (Logging.On)
     {
         Logging.PrintInfo(Logging.Web, this, ".ctor", "host=" + destinationHost + ", #certs=" + ((clientCertificates == null) ? "null" : clientCertificates.Count.ToString(NumberFormatInfo.InvariantInfo)));
     }
     this.m_ExceptionStatus = WebExceptionStatus.SecureChannelFailure;
     this.m_Worker = new SslState(networkStream, initiatingRequest is HttpWebRequest, SettingsSectionInternal.Section.EncryptionPolicy);
     this.m_DestinationHost = destinationHost;
     this.m_ClientCertificates = clientCertificates;
     RemoteCertValidationCallback certValidationCallback = servicePoint.SetupHandshakeDoneProcedure(this, initiatingRequest);
     this.m_Worker.SetCertValidationDelegate(certValidationCallback);
 }
예제 #47
0
파일: HttpReq.cs 프로젝트: 0xDAD/pr2sms
        public static string Send(string request)
        {
            try
            {

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(request);

                req.ReadWriteTimeout = 9000;
                req.Timeout = 10000;

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                status = KannelReqCode.SuccessfullyAccepted;
                // Get the stream associated with the response.
                Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

                string answer = readStream.ReadLine();
                //MessageBox.Show(response.StatusCode.ToString() + "\n" + readStream.ReadLine(), "SMS Sender", MessageBoxButtons.OK, MessageBoxIcon.Information);

                response.Close();
                readStream.Close();
                return answer;
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.Timeout)
                    status = KannelReqCode.OperationTimeout;
                else
                    status = KannelReqCode.UnknownError;
                wes = ex.Status;
                return ex.Message;
            }
            catch(Exception ex)
            {
                wes = WebExceptionStatus.SendFailure;
                status = KannelReqCode.UnknownError;
                return ex.Message;
            }
        }
예제 #48
0
        private void ParseException(WebException exception)
        {
            _status = exception.Status;
            string responseText = String.Empty;

            using (StreamReader r = new StreamReader(exception.Response.GetResponseStream()))
            {
                responseText = r.ReadToEnd();
            }

            _exceptionResponse = responseText;
            XElement element = XElement.Parse(responseText);

            if (element != null)
                foreach (var s in element.DescendantsAndSelf("error"))
                {
                    _responseErrorText = s.Value;
                }

            element = null;
        }
 private static RollbarResponseCode ToRollbarStatus(WebExceptionStatus status) {
     switch ((int) status) {
         case 200:
             return RollbarResponseCode.Success;
         case 400:
             return RollbarResponseCode.BadRequest;
         case 401:
             return RollbarResponseCode.Unauthorized;
         case 403:
             return RollbarResponseCode.AccessDenied;
         case 413:
             return RollbarResponseCode.RequestTooLarge;
         case 422:
             return RollbarResponseCode.UnprocessablePayload;
         case 429:
             return RollbarResponseCode.TooManyRequests;
         case 500:
             return RollbarResponseCode.InternalServerError;
         default:
             throw new ArgumentException("Invalid Status returned from Rollbar", "status");
     }
 }
예제 #50
0
        internal RESTResponse(HttpWebResponse response, long elapsedTime = 0, string error = null, WebExceptionStatus errorStatus = WebExceptionStatus.Success)
        {
            if (!object.ReferenceEquals(response, null))
            {
                this.Content = this.GetContent(response);
                this.ContentEncoding = response.ContentEncoding;
                this.ContentLength = response.ContentLength;
                this.ContentType = response.ContentType;
                this.Cookies = response.Cookies;
                this.Headers = response.Headers;
                this.ResponseStatus = response.StatusCode.ToString();
                this.ResponseUri = response.ResponseUri;
                this.ReturnedError = (this.ErrorStatus.Equals(WebExceptionStatus.Success)) ? false : true;
                this.Server = response.Server;
                this.StatusCode = response.StatusCode;
                this.StatusDescription = response.StatusDescription;
            }

            this.ElapsedTime = elapsedTime;
            this.Error = error;
            this.ErrorStatus = errorStatus;
        }
예제 #51
0
파일: _netres.cs 프로젝트: ArildF/masters
        /*++

            GetWebStatusString - Get a WebExceptionStatus-specific resource string


            This method takes an input string and a WebExceptionStatus. We use the input
            string as a key to find a status message and the webStatus to produce
            a status-specific message, then we combine the two.

            Input:

                Res             - Id for resource string.
                Status          - The WebExceptionStatus to be formatted.

            Returns:

                string for localized message.

        --*/
        public static string GetWebStatusString(string Res, WebExceptionStatus Status) {
            string Msg;
            string StatusEnumName;
            string StatusMsg;

            // First, convert the WebExceptionStatus to its label.

            StatusEnumName = ((Enum)Status).ToString();

            // Now combine the label with the base enum key and look up the
            // status msg.

            StatusMsg = SR.GetString("net_webstatus_" + StatusEnumName);

            // Get the base status.

            Msg = SR.GetString(Res);

            // Format the status specific message into the base status and return
            // that

            return String.Format(Msg, StatusMsg);
        }
예제 #52
0
		void HandleError (WebExceptionStatus st, Exception e, string where)
		{
			status = st;
			lock (this) {
				if (st == WebExceptionStatus.RequestCanceled)
					Data = new WebConnectionData ();
			}

			if (e == null) { // At least we now where it comes from
				try {
#if TARGET_JVM
					throw new Exception ();
#else
					throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
#endif
				} catch (Exception e2) {
					e = e2;
				}
			}

			HttpWebRequest req = null;
			if (Data != null && Data.request != null)
				req = Data.request;

			Close (true);
			if (req != null) {
				req.FinishedReading = true;
				req.SetResponseError (st, e, where);
			}
		}
예제 #53
0
 public WebException(string message, WebExceptionStatus status)
     : base(message)
 {
     this.status = status;
 }
예제 #54
0
		internal Stream CreateStream (byte[] buffer)
		{
			sslStream = provider.CreateSslStream (networkStream, false, settings);

			try {
				sslStream.AuthenticateAsClient (
					request.Address.Host, request.ClientCertificates,
					(SslProtocols)ServicePointManager.SecurityProtocol,
					ServicePointManager.CheckCertificateRevocationList);

				status = WebExceptionStatus.Success;
			} catch (Exception ex) {
				status = WebExceptionStatus.SecureChannelFailure;
				throw;
			} finally {
				if (CertificateValidationFailed)
					status = WebExceptionStatus.TrustFailure;

				if (status == WebExceptionStatus.Success)
					request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
				else {
					request.ServicePoint.UpdateClientCertificate (null);
					sslStream = null;
				}
			}

			try {
				if (buffer != null)
					sslStream.Write (buffer, 0, buffer.Length);
			} catch {
				status = WebExceptionStatus.SendFailure;
				sslStream = null;
				throw;
			}

			return sslStream.AuthenticatedStream;
		}
예제 #55
0
		public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
		{
			this.request = request;
			this.networkStream = networkStream;

			settings = request.TlsSettings;
			provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
			status = WebExceptionStatus.SecureChannelFailure;

			validationHelper = ChainValidationHelper.Create (provider.Provider, ref settings, this);
		}
예제 #56
0
 public static string GetWebStatusString(WebExceptionStatus Status) {
     return SR.GetString(WebExceptionMapping.GetWebStatusString(Status));
 }
예제 #57
0
		internal bool EndWrite (HttpWebRequest request, IAsyncResult result)
		{
			if (request.FinishedReading)
				return true;

			Stream s = null;
			lock (this) {
				if (Data.request != request)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				if (nstream == null)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				s = nstream;
			}

			try {
				s.EndWrite (result);
				return true;
			} catch {
				status = WebExceptionStatus.SendFailure;
				return false;
			}
		}
예제 #58
0
		internal void EndWrite2 (HttpWebRequest request, IAsyncResult result)
		{
			if (request.FinishedReading)
				return;

			Stream s = null;
			lock (this) {
				if (Data.request != request)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				if (nstream == null)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				s = nstream;
			}

			try {
				s.EndWrite (result);
			} catch (Exception exc) {
				status = WebExceptionStatus.SendFailure;
				if (exc.InnerException != null)
					throw exc.InnerException;
				throw;
			}
		}
예제 #59
0
		internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
		{
			Stream s = null;
			lock (this) {
				if (Data.request != request)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				if (nstream == null)
					return null;
				s = nstream;
			}

			IAsyncResult result = null;
			try {
				result = s.BeginWrite (buffer, offset, size, cb, state);
			} catch (Exception) {
				status = WebExceptionStatus.SendFailure;
				throw;
			}

			return result;
		}
예제 #60
0
		internal EventHandler SendRequest (HttpWebRequest request)
		{
			if (request.Aborted)
				return null;

			lock (this) {
				if (!busy) {
					busy = true;
					status = WebExceptionStatus.Success;
					ThreadPool.QueueUserWorkItem (initConn, request);
				} else {
					lock (queue) {
						queue.Enqueue (request);
					}
				}
			}

			return abortHandler;
		}