public static UsersResult fromResponse(ResponseWrapper responseWrapper)
 {
     UsersResult usersResult = new UsersResult();
     if (responseWrapper.isServerResponse()) {
         usersResult = JsonConvert.DeserializeObject<UsersResult> (responseWrapper.responseContent);
     }
     usersResult.ResponseResult=responseWrapper;
     return usersResult;
 }
 public static TagAliasResult fromResponse(ResponseWrapper responseWrapper)
 {
     TagAliasResult tagAliasResult = new TagAliasResult();
     if (responseWrapper.isServerResponse())
     {
         tagAliasResult = JsonConvert.DeserializeObject<TagAliasResult>(responseWrapper.responseContent);
     }
     tagAliasResult.ResponseResult = responseWrapper;
     return tagAliasResult;
 }
 public static MessagesResult fromResponse(ResponseWrapper responseWrapper)
 {
     MessagesResult receivedsResult = new MessagesResult();
     if (responseWrapper.responseCode==HttpStatusCode.OK)
     {
         receivedsResult.messages = JsonConvert.DeserializeObject<List<Message>>(responseWrapper.responseContent);
     }
     receivedsResult.ResponseResult = responseWrapper;
     return receivedsResult;
 }
 public static AliasDeviceListResult fromResponse(ResponseWrapper responseWrapper)
 {
     AliasDeviceListResult aliasDeviceListResult = new AliasDeviceListResult();
     if (responseWrapper.isServerResponse())
     {
         aliasDeviceListResult = JsonConvert.DeserializeObject<AliasDeviceListResult>(responseWrapper.responseContent);
     }
     aliasDeviceListResult.ResponseResult = responseWrapper;
     return aliasDeviceListResult;
 }
 public static MessagesResult fromResponse(ResponseWrapper responseWrapper)
 {
     MessagesResult receivedsResult = new MessagesResult();
     if (responseWrapper.responseCode==HttpStatusCode.OK)
     {
        receivedsResult.messages = JsonConvert.DeserializeObject<List<Message>>(responseWrapper.responseContent);
        //[{"android":{"received":null,"target":null,"online_push":null,"click":null,"msg_click":null},"ios":{"apns_sent":1,"apns_target":1,"click":null,"target":null,"received":null,"msg_click":null},"winphone":{"mpns_target":null,"mpns_sent":null,"click":null},"msg_id":991969761}]
     }
     receivedsResult.ResponseResult = responseWrapper;
     return receivedsResult;
 }
Пример #6
0
        /**
         *
         * method "POST" or "GET"
         * url
         * auth   可选
         */
        public ResponseWrapper sendRequest(String method, String url, String auth,String reqParams)
        {
            Console.WriteLine("Send request - " + method.ToString() + " " + url + " "+ DateTime.Now);
            if (null != reqParams)
            {
                Console.WriteLine("Request Content - " + reqParams +" "+ DateTime.Now);
            }
            ResponseWrapper result = new ResponseWrapper();
            HttpWebRequest myReq = null;
            HttpWebResponse response = null;
            try
            {
                myReq = (HttpWebRequest)WebRequest.Create(url);
                myReq.Method = method;
                myReq.ContentType = "application/json";
                if ( !String.IsNullOrEmpty(auth) )
                {
                    myReq.Headers.Add("Authorization", "Basic " + auth);
                }
                if (method == "POST")
                {
                    byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
                    myReq.ContentLength = bs.Length;
                    using (Stream reqStream = myReq.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                        reqStream.Close();
                    }
                }
                response = (HttpWebResponse)myReq.GetResponse();
                HttpStatusCode statusCode = response.StatusCode;
                result.responseCode = statusCode;
                if (Equals(response.StatusCode, HttpStatusCode.OK))
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = reader.ReadToEnd();
                    }
                    String limitQuota = response.GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = response.GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset = response.GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Console.WriteLine("Succeed to get response - 200 OK" +" "+ DateTime.Now);
                    Console.WriteLine("Response Content - {0}", result.responseContent +" "+ DateTime.Now);
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode errorCode = ((HttpWebResponse)e.Response).StatusCode;
                    string statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
                    using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = sr.ReadToEnd();
                    }
                    result.responseCode = errorCode;
                    result.exceptionString = e.Message;
                    String limitQuota = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Debug.Print(e.Message);
                    result.setErrorObject();
                    Console.WriteLine(string.Format("fail  to get response - {0}", errorCode) + " "+ DateTime.Now);
                    Console.WriteLine(string.Format("Response Content - {0}", result.responseContent) + " "+ DateTime.Now);

                    throw new APIRequestException(result);
                }
                else
                {//
                    throw new APIConnectionException(e.Message);
                }

            }
            //这里不再抓取非http的异常,如果异常抛出交给开发者自行处理
            //catch (System.Exception ex)
            //{
            //     String errorMsg = ex.Message;
            //     Debug.Print(errorMsg);
            //}
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if(myReq != null)
                {
                    myReq.Abort();
                }
            }
            return result;
        }
Пример #7
0
        /**
         *
         * method "POST" or "GET"
         * url
         * auth   可选
         */
        public ResponseWrapper sendRequest(String method, String url, String auth, String reqParams)
        {
            Console.WriteLine("Send request - " + method.ToString() + " " + url + " " + DateTime.Now);
            if (null != reqParams)
            {
                Console.WriteLine("Request Content - " + reqParams + " " + DateTime.Now);
            }
            //结果wrap
            ResponseWrapper result = new ResponseWrapper();
            //创建httprequest
            HttpWebRequest myReq = null;
            //创建httpresponse
            HttpWebResponse response = null;

            try
            {
                //利用工厂机制(factory mechanism)通过Create()方法来创建的
                myReq = (HttpWebRequest)WebRequest.Create(url);
                //request类型
                myReq.Method      = method;
                myReq.ContentType = "application/json";
                myReq.KeepAlive   = false;
                //auth是否为null或者空
                if (!String.IsNullOrEmpty(auth))
                {
                    //添加头auth
                    myReq.Headers.Add("Authorization", "Basic " + auth);
                }
                if (method == "POST")
                {
                    //utf8编码
                    byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
                    myReq.ContentLength = bs.Length;
                    using (Stream reqStream = myReq.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                        reqStream.Close();
                    }
                }

                if (method == "PUT")
                {
                    //utf8编码
                    byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
                    myReq.ContentLength = bs.Length;
                    using (Stream reqStream = myReq.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                        reqStream.Close();
                    }
                }

                //response
                response = (HttpWebResponse)myReq.GetResponse();
                //http status code
                HttpStatusCode statusCode = response.StatusCode;
                result.responseCode = statusCode;
                if (Equals(response.StatusCode, HttpStatusCode.OK))
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = reader.ReadToEnd();
                    }
                    String limitQuota     = response.GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = response.GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset     = response.GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Console.WriteLine("Succeed to get response - 200 OK" + " " + DateTime.Now);
                    Console.WriteLine("Response Content - {0}", result.responseContent + " " + DateTime.Now);
                }
            }
            //异常处理
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode errorCode         = ((HttpWebResponse)e.Response).StatusCode;
                    string         statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
                    using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = sr.ReadToEnd();
                    }
                    result.responseCode    = errorCode;
                    result.exceptionString = e.Message;
                    String limitQuota     = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset     = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Debug.Print(e.Message);
                    result.setErrorObject();
                    Console.WriteLine(string.Format("fail  to get response - {0}", errorCode) + " " + DateTime.Now);
                    Console.WriteLine(string.Format("Response Content - {0}", result.responseContent) + " " + DateTime.Now);

                    throw new APIRequestException(result);
                }
                else
                {
                    String info = method + url + auth + reqParams;
                    Console.WriteLine(info);
                    throw new APIConnectionException(e.Message, info);
                }
            }
            //这里不再抓取非http的异常,如果异常抛出交给开发者自行处理
            //catch (System.Exception ex)
            //{
            //     String errorMsg = ex.Message;
            //     Debug.Print(errorMsg);
            //}
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (myReq != null)
                {
                    myReq.Abort();
                }
            }
            return(result);
        }
Пример #8
0
        public ResponseWrapper sendRequest(
            string method,
            string url,
            string auth,
            string reqParams)
        {
            Console.WriteLine("Send request - " + method.ToString() + " " + url + " " + (object)DateTime.Now);
            if (reqParams != null)
            {
                Console.WriteLine("Request Content - " + reqParams + " " + (object)DateTime.Now);
            }
            ResponseWrapper responseRequest = new ResponseWrapper();
            HttpWebRequest  httpWebRequest  = (HttpWebRequest)null;
            HttpWebResponse httpWebResponse = (HttpWebResponse)null;

            try
            {
                httpWebRequest             = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Method      = method;
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.KeepAlive   = false;
                if (!string.IsNullOrEmpty(auth))
                {
                    httpWebRequest.Headers.Add("Authorization", "Basic " + auth);
                }
                if (method == "POST" || method == "PUT")
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(reqParams);
                    httpWebRequest.ContentLength = (long)bytes.Length;
                    using (Stream requestStream = httpWebRequest.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, bytes.Length);
                        requestStream.Close();
                    }
                }
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                HttpStatusCode statusCode = httpWebResponse.StatusCode;
                responseRequest.responseCode = statusCode;
                if (object.Equals((object)httpWebResponse.StatusCode, (object)HttpStatusCode.OK))
                {
                    using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8))
                        responseRequest.responseContent = streamReader.ReadToEnd();
                    string responseHeader1 = httpWebResponse.GetResponseHeader("X-Rate-Limit-Limit");
                    string responseHeader2 = httpWebResponse.GetResponseHeader("X-Rate-Limit-Remaining");
                    string responseHeader3 = httpWebResponse.GetResponseHeader("X-Rate-Limit-Reset");
                    responseRequest.setRateLimit(responseHeader1, responseHeader2, responseHeader3);
                    Console.WriteLine("Succeed to get response - 200 OK " + (object)DateTime.Now);
                    Console.WriteLine("Response Content - {0}", (object)(responseRequest.responseContent + " " + (object)DateTime.Now));
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode statusCode        = ((HttpWebResponse)ex.Response).StatusCode;
                    string         statusDescription = ((HttpWebResponse)ex.Response).StatusDescription;
                    using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8))
                        responseRequest.responseContent = streamReader.ReadToEnd();
                    responseRequest.responseCode    = statusCode;
                    responseRequest.exceptionString = ex.Message;
                    string responseHeader1 = ((HttpWebResponse)ex.Response).GetResponseHeader("X-Rate-Limit-Limit");
                    string responseHeader2 = ((HttpWebResponse)ex.Response).GetResponseHeader("X-Rate-Limit-Remaining");
                    string responseHeader3 = ((HttpWebResponse)ex.Response).GetResponseHeader("X-Rate-Limit-Reset");
                    responseRequest.setRateLimit(responseHeader1, responseHeader2, responseHeader3);
                    Debug.Print(ex.Message);
                    responseRequest.setErrorObject();
                    Console.WriteLine(string.Format("fail  to get response - {0}", (object)statusCode) + " " + (object)DateTime.Now);
                    Console.WriteLine(string.Format("Response Content - {0}", (object)responseRequest.responseContent) + " " + (object)DateTime.Now);
                    throw new APIRequestException(responseRequest);
                }
                string info = method + url + auth + reqParams;
                Console.WriteLine(info);
                throw new APIConnectionException(ex.Message, info);
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
            }
            return(responseRequest);
        }
Пример #9
0
        /**
         *
         * method "POST" or "GET"
         * url
         * auth   可选
         */
        public ResponseWrapper sendRequest(String method, String url, String auth, String reqParams)
        {
            Console.WriteLine("Send request - " + method.ToString() + " " + url + " " + DateTime.Now);
            if (null != reqParams)
            {
                Console.WriteLine("Request Content - " + reqParams + " " + DateTime.Now);
            }
            ResponseWrapper result   = new ResponseWrapper();
            HttpWebRequest  myReq    = null;
            HttpWebResponse response = null;

            try
            {
                myReq             = (HttpWebRequest)WebRequest.Create(url);
                myReq.Method      = method;
                myReq.ContentType = "application/json";
                if (!String.IsNullOrEmpty(auth))
                {
                    myReq.Headers.Add("Authorization", "Basic " + auth);
                }
                if (method == "POST")
                {
                    byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
                    myReq.ContentLength = bs.Length;
                    using (Stream reqStream = myReq.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                        reqStream.Close();
                    }
                }
                response = (HttpWebResponse)myReq.GetResponse();
                HttpStatusCode statusCode = response.StatusCode;
                result.responseCode = statusCode;
                if (Equals(response.StatusCode, HttpStatusCode.OK))
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = reader.ReadToEnd();
                    }
                    String limitQuota     = response.GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = response.GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset     = response.GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Console.WriteLine("Succeed to get response - 200 OK" + " " + DateTime.Now);
                    Console.WriteLine("Response Content - {0}", result.responseContent + " " + DateTime.Now);
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode errorCode         = ((HttpWebResponse)e.Response).StatusCode;
                    string         statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
                    using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), System.Text.Encoding.UTF8))
                    {
                        result.responseContent = sr.ReadToEnd();
                    }
                    result.responseCode    = errorCode;
                    result.exceptionString = e.Message;
                    String limitQuota     = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_QUOTA);
                    String limitRemaining = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Remaining);
                    String limitReset     = ((HttpWebResponse)e.Response).GetResponseHeader(RATE_LIMIT_Reset);
                    result.setRateLimit(limitQuota, limitRemaining, limitReset);
                    Debug.Print(e.Message);
                    result.setErrorObject();
                    //DTcms.Common.Log.Error(string.Format("fail  to get response code - {0}, content - {1}, \r\nsource: {2}", errorCode, result.responseContent, reqParams));

                    throw new APIRequestException(result);
                }
                else
                {//
                    //DTcms.Common.Log.Error(string.Format("error : {0}, \r\nsource : {1}",e.Message,reqParams));
                    throw new APIConnectionException(e.Message);
                }
            }
            //这里不再抓取非http的异常,如果异常抛出交给开发者自行处理
            //catch (System.Exception ex)
            //{
            //     String errorMsg = ex.Message;
            //     Debug.Print(errorMsg);
            //}
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (myReq != null)
                {
                    myReq.Abort();
                }
            }
            return(result);
        }
Пример #10
0
 public APIRequestException(ResponseWrapper responseRequest)
     : base(responseRequest.exceptionString)
 {
     this.responseRequest = responseRequest;
 }
 public APIRequestException(ResponseWrapper responseRequest)
     : base(responseRequest.exceptionString)
 {
     this.responseRequest = responseRequest;
 }