示例#1
0
        private bool HttpPostData <T>(string operating, string relativeUrl, object data, out T returnData, out RestServiceError error, bool isDynamic = false)
        {
            string      url     = CombineUrl(this.m_ServicePath, relativeUrl == null || relativeUrl.Trim().Length <= 0 ? string.Empty : relativeUrl);
            HttpRequest request = new HttpRequest(url, ContentType, ContentType);

            if (this.Timeout.HasValue)
            {
                request.Timeout = this.Timeout.Value;
            }
            HandlePostRequestHeader(request, operating);
            request.Method = Operating.POST;
            if (data != null)
            {
                ISerializer serializer = SerializerFactory.GetSerializer(request.ContentType);
                if (serializer != null)
                {
                    string postContent = serializer.Serialization(data, data.GetType());
                    using (StreamWriter stream = new StreamWriter(request.GetRequestStream()))
                    {
                        stream.Write(postContent);
                    }
                }
            }
            return(GetResponse(request, out returnData, out error, isDynamic));
        }
示例#2
0
        private IAsyncHandle AsyncHttpPostData <T>(string operating, string relativeUrl, object data, Action <T> callback, Action <RestServiceError> errorHandler, bool isDynamic = false)
        {
            string      url     = CombineUrl(this.m_ServicePath, relativeUrl == null || relativeUrl.Trim().Length <= 0 ? string.Empty : relativeUrl);
            HttpRequest request = new HttpRequest(url, ContentType, ContentType);

            HandlePostRequestHeader(request, operating);
            request.Method = Operating.POST;
            if (data != null)
            {
                ISerializer serializer = SerializerFactory.GetSerializer(request.ContentType);
                if (serializer != null)
                {
                    string postContent = serializer.Serialization(data, data.GetType());
                    using (StreamWriter stream = new StreamWriter(request.GetRequestStream()))
                    {
                        stream.Write(postContent);
                    }
                }
            }
            IAsyncResult handler = request.BeginGetResponse(new AsyncCallback(OnGetResponse <T>),
                                                            new AsyncArgs <T>(request, callback, errorHandler, isDynamic));

            return(new RestClientAsyncHandle(request, handler));
        }
示例#3
0
 private bool GetResponse <T>(HttpRequest request, out T data, out RestServiceError error, bool isDynamic)
 {
     try
     {
         using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
         {
             ISerializer serializer = SerializerFactory.GetSerializer((request.Accept == null || request.Accept.Length == 0) ? ContentType : request.Accept);
             string      responseTxt;
             using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
             {
                 responseTxt = readStream.ReadToEnd();
             }
             if (!ErrorHandle(out error, responseTxt, serializer))
             {
                 if (isDynamic)
                 {
                     data = DynamicXml.Parse(responseTxt);
                 }
                 else
                 {
                     if (!string.IsNullOrEmpty(responseTxt))
                     {
                         using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt)))
                         {
                             data = (T)serializer.Deserialize(stream, typeof(T));
                         }
                     }
                     else
                     {
                         data = default(T);
                     }
                 }
                 error = null;
                 return(true);
             }
         }
     }
     catch (WebException ex)
     {
         HttpWebResponse e = ex.Response as HttpWebResponse;
         error                   = new RestServiceError();
         error.StatusCode        = e.StatusCode.GetHashCode();
         error.StatusDescription = e.StatusDescription;
         error.Faults            = new List <Error>();
         error.Faults.Add(new Error()
         {
             ErrorCode        = "00000",
             ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", request.RequestUri.ToString(), ex.ToString())
         });
         e.Close();
         ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", request.RequestUri.ToString()), new object[0]);
     }
     catch (Exception ex)
     {
         error            = new RestServiceError();
         error.StatusCode = 500;
         error.Faults     = new List <Error>();
         error.Faults.Add(new Error()
         {
             ErrorCode        = "00000",
             ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", request.RequestUri.ToString(), ex.ToString())
         });
         ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", request.RequestUri.ToString()), new object[0]);
     }
     data = default(T);
     return(false);
 }
示例#4
0
        private void OnGetResponse <T>(IAsyncResult result)
        {
            AsyncArgs <T>    args     = (AsyncArgs <T>)result.AsyncState;
            HttpWebResponse  response = null;
            object           data     = null;
            RestServiceError error    = null;

            try
            {
                response = args.Request.EndGetResponse(result) as HttpWebResponse;
                ISerializer serializer = SerializerFactory.GetSerializer((args.Request.Accept == null || args.Request.Accept.Length == 0) ? ContentType : args.Request.Accept);
                string      responseTxt;
                using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    responseTxt = readStream.ReadToEnd();
                }
                if (!ErrorHandle(out error, responseTxt, serializer))
                {
                    if (args.IsDynamic)
                    {
                        data = DynamicXml.Parse(responseTxt);
                    }
                    else
                    {
                        using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt)))
                        {
                            data = (T)serializer.Deserialize(stream, typeof(T));
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse e = ex.Response as HttpWebResponse;
                error                   = new RestServiceError();
                error.StatusCode        = e.StatusCode.GetHashCode();
                error.StatusDescription = e.StatusDescription;
                error.Faults            = new List <Error>();
                error.Faults.Add(new Error()
                {
                    ErrorCode        = "00000",
                    ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString())
                });
                e.Close();
                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]);
            }
            catch (Exception ex)
            {
                error            = new RestServiceError();
                error.StatusCode = 500;
                error.Faults     = new List <Error>();
                error.Faults.Add(new Error()
                {
                    ErrorCode        = "00000",
                    ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString())
                });
                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            if (error == null)
            {
                Action <T> handler = args.SucceedHandler;
                if (handler != null)
                {
                    handler((T)data);
                }
            }
            else
            {
                Action <RestServiceError> eHandler = args.ErrorHandler;
                if (eHandler != null)
                {
                    eHandler(error);
                }
            }
        }