public setRateLimit ( String quota, String remaining, String reset ) : void | ||
quota | String | |
remaining | String | |
reset | String | |
return | void |
/** * * 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; }
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); }
/** * * 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); }
/** * * 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); }