예제 #1
0
 public ConnectionStatus DeleteSync(string path)
 {
     var connection = this.CreateConnection(path, "DELETE");
     connection.Timeout = this._ConnectionSettings.TimeOut;
     WebResponse response = null;
     try
     {
         response = connection.GetResponse();
         var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
         response.Close();
         return new ConnectionStatus(result);
     }
     catch (WebException e)
     {
         ConnectionError error;
         if (e.Status == WebExceptionStatus.Timeout)
         {
             error = new ConnectionError(e) { HttpStatusCode = HttpStatusCode.InternalServerError };
         }
         else
         {
             error = new ConnectionError(e);
         }
         return new ConnectionStatus(error);
     }
     catch (Exception e) { return new ConnectionStatus(new ConnectionError(e)); }
     finally
     {
         if (response != null)
             response.Close();
     }
 }
예제 #2
0
        public void RaiseCallBack(Exception e)
        {
            var error = new ConnectionError(e);

            if (e is WebException)
            {
                error.Type = ConnectionErrorType.Server;
                var oe = (WebException)e;
                if (oe.Response != null)
                {
                    error.HttpStatusCode = ((System.Net.HttpWebResponse)(oe.Response)).StatusCode;

                }
                else
                {
                    error.ExceptionMessage = "Could not connect to server: " + Connection.Address.ToString();
                }
            }
            else if (e is Exception)
                error.Type = ConnectionErrorType.Client;

            if (!this.RaisedCallback)
            {
                this.RaisedCallback = true;
                if (this.Callback != null)
                    this.Callback(new ConnectionStatus(error));
            }
        }
예제 #3
0
        public ConnectionStatus PostSync(string path, string data)
        {
            var connection = this.CreateConnection(path);

            connection.Timeout = this._ConnectionSettings.TimeOut;
            connection.Method  = "POST";
            Stream      postStream = null;
            WebResponse response   = null;

            try
            {
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                connection.ContentLength = buffer.Length;
                postStream = connection.GetRequestStream();
                postStream.Write(buffer, 0, buffer.Length);
                postStream.Close();
                response = connection.GetResponse();
                var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
                response.Close();
                return(new ConnectionStatus(result));
            }
            catch (WebException e)
            {
                ConnectionError error;
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    error = new ConnectionError(e)
                    {
                        HttpStatusCode = HttpStatusCode.InternalServerError
                    };
                }
                else
                {
                    error = new ConnectionError(e);
                }
                return(new ConnectionStatus(error));
            }
            catch (Exception e) { return(new ConnectionStatus(new ConnectionError(e))); }
            finally
            {
                if (postStream != null)
                {
                    postStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }
예제 #4
0
파일: Connection.cs 프로젝트: alincak/NEST
        public ConnectionStatus DeleteSync(string path)
        {
            var connection = this.CreateConnection(path, "DELETE");

            connection.Timeout = this._ConnectionSettings.TimeOut;
            WebResponse response = null;

            try
            {
                response = connection.GetResponse();
                var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
                response.Close();
                return(new ConnectionStatus(result));
            }
            catch (WebException e)
            {
                ConnectionError error;
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    error = new ConnectionError(e)
                    {
                        HttpStatusCode = HttpStatusCode.InternalServerError
                    };
                }
                else
                {
                    error = new ConnectionError(e);
                }
                return(new ConnectionStatus(error));
            }
            catch (Exception e) { return(new ConnectionStatus(new ConnectionError(e))); }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }
예제 #5
0
 public ConnectionStatus PostSync(string path, string data)
 {
     var connection = this.CreateConnection(path);
     connection.Timeout = this._ConnectionSettings.TimeOut;
     connection.Method = "POST";
     Stream postStream = null;
     WebResponse response = null;
     try
     {
         byte[] buffer = Encoding.UTF8.GetBytes(data);
         connection.ContentLength = buffer.Length;
         postStream = connection.GetRequestStream();
         postStream.Write(buffer, 0, buffer.Length);
         postStream.Close();
         response = connection.GetResponse();
         var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
         response.Close();
         return new ConnectionStatus(result);
     }
     catch (WebException e)
     {
         ConnectionError error;
         if (e.Status == WebExceptionStatus.Timeout)
         {
             error = new ConnectionError(e) { HttpStatusCode = HttpStatusCode.InternalServerError };
         }
         else
         {
             error = new ConnectionError(e);
         }
         return new ConnectionStatus(error);
     }
     catch (Exception e) { return new ConnectionStatus(new ConnectionError(e)); }
     finally
     {
         if (postStream != null)
             postStream.Close();
         if (response != null)
             response.Close();
     }
 }
예제 #6
0
        private ConnectionError GetConnectionErrorFromWebException(WebException e, out string result)
        {
            result = "";
            using (var r = e.Response)
            {

                if (e.Response != null)
                {
                    using (var d = e.Response.GetResponseStream())
                    {
                        result = new StreamReader(d).ReadToEnd();
                    }
                }

                ConnectionError error;
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    error = new ConnectionError(e, result)
                    {
                        HttpStatusCode = HttpStatusCode.InternalServerError
                    };
                }
                else
                {
                    error = new ConnectionError(e, result);
                }
                return error;
            }
        }
예제 #7
0
 public ConnectionStatus(ConnectionError error)
 {
     this.Success = false;
     this.Error   = error;
 }
예제 #8
0
 public ConnectionStatus(ConnectionError error)
 {
     this.Success = false;
     this.Error = error;
 }