示例#1
0
        private RestResponce GetResponce(RestResponce responce)
        {
            try
            {
                var webResponse = _webRequest.GetResponse() as HttpWebResponse;
                if (webResponse != null)
                {
                    using (var reader = new StreamReader(webResponse.GetResponseStream()))
                    {
                        responce.StatusCode   = webResponse.StatusCode;
                        responce.StatusNumber = Convert.ToInt32(webResponse.StatusCode);
                        responce.Headers      = GetResponceHeaders(webResponse);
                        responce.Content      = reader.ReadToEnd();
                    }
                    webResponse.Close();
                }
            }
            catch (WebException ex)
            {
                var resp = ex.Response as HttpWebResponse;
                responce.StatusCode   = resp.StatusCode;
                responce.StatusNumber = Convert.ToInt32(resp.StatusCode);
                responce.Headers      = GetResponceHeaders(resp);
                responce.Content      = string.Empty;
                resp.Close();
            }

            return(responce);
        }
示例#2
0
        public RestResponce <T> Execute <T>(RestRequest request, Serialize serialize = Serialize.Json) where T : new()
        {
            PrepareRequest(request);
            RestResponce <T> responce = null;

            switch (serialize)
            {
            case Serialize.Xml:
                responce = new RestResponce <T>(new SerializeToXml());
                break;

            case Serialize.Json:
                responce = new RestResponce <T>(new SerializeToJson());
                break;

            default:
                responce = new RestResponce <T>();
                break;
            }
            var resp = GetResponce(responce);

            return((RestResponce <T>)resp);
        }