Exemplo n.º 1
0
        public static UsersResult fromResponse(ResponseWrapper responseWrapper)
        {
            UsersResult usersResult = new UsersResult();
            if (responseWrapper.isServerResponse()) {
                usersResult = JsonConvert.DeserializeObject<UsersResult> (responseWrapper.responseContent);
            }
            usersResult.ResponseResult=responseWrapper;
            return usersResult;
	    }
Exemplo n.º 2
0
 public static AliasDeviceListResult fromResponse(ResponseWrapper responseWrapper)
 {
     AliasDeviceListResult aliasDeviceListResult = new AliasDeviceListResult();
     if (responseWrapper.isServerResponse())
     {
         aliasDeviceListResult = JsonConvert.DeserializeObject<AliasDeviceListResult>(responseWrapper.responseContent);
     }
     aliasDeviceListResult.ResponseResult = responseWrapper;
     return aliasDeviceListResult;
 }
Exemplo n.º 3
0
 public static TagAliasResult fromResponse(ResponseWrapper responseWrapper)
 {
     TagAliasResult tagAliasResult = new TagAliasResult();
     if (responseWrapper.isServerResponse())
     {
         tagAliasResult = JsonConvert.DeserializeObject<TagAliasResult>(responseWrapper.responseContent);
     }
     tagAliasResult.ResponseResult = responseWrapper;
     return tagAliasResult;
 }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 public APIRequestException(ResponseWrapper responseRequest)
     : base(responseRequest.exceptionString)
 {
     this.responseRequest = responseRequest;
 }
Exemplo n.º 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;
        }
Exemplo n.º 7
0
 public APIRequestException(ResponseWrapper responseRequest)
     : base(responseRequest.exceptionString)
 {
     this.responseRequest = responseRequest;
 }
Exemplo n.º 8
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);
        }