Пример #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
        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;
 }