JSON

Rest API 开发 学习笔记(2)

字号+ 作者:H5之家 来源:H5之家 2018-07-18 08:23 我要评论( )

接下来处理HandleRequest: /// summary/// Handles an http request for an Api call./// /summarypublic RestHandlerResponse HandleRequest(RestHandlerRequest rhr){RestHandlerResponse res;// 50 Requests in

接下来处理HandleRequest:

/// <summary> /// Handles an http request for an Api call. /// </summary> public RestHandlerResponse HandleRequest(RestHandlerRequest rhr) { RestHandlerResponse res; // 50 Requests in maximum if (!this.restProcessorSemaphore.WaitOne(0)) { res = new RestHandlerResponse(503); } else { try { // There is no need decode the url here, since the address will be decoded when it is parsed. //rhr.Address = System.Web.HttpUtility.UrlDecode(rhr.Address); res = this.process(rhr); } catch (RestManagerException ex) { traceLog.ErrorFormat("Error happened while processing request\n{1}.\nException info:\n{0} ",ex.Message); res = new RestHandlerResponse(500); } try { this.restProcessorSemaphore.Release(); } catch (System.Threading.SemaphoreFullException) { traceLog.ErrorFormat("Error happened while processing Semaphore.Release"); } catch (System.IO.IOException) { traceLog.ErrorFormat("Error happened while processing Semaphore.Release"); } catch (System.UnauthorizedAccessException) { traceLog.ErrorFormat("Error happened while processing Semaphore.Release"); } } return res; }

 

 

接下来我们写发送请求代码:

private JObject MakeRequest(string url) { var subsequentRequest = WebRequest.Create(url) as HttpWebRequest; subsequentRequest.Timeout = 30000; subsequentRequest.Headers.Add("Authorization", "OAuth " + TestToken); subsequentRequest.Headers.Add("App-User", TestUserName); WebResponse subsequentResponse; try { subsequentResponse = subsequentRequest.GetResponse(); Stream stream = subsequentResponse.GetResponseStream(); StreamReader sr = new StreamReader(stream); string output = sr.ReadToEnd(); JObject jsonStr = JObject.Parse(output); return jsonStr; } catch (WebException ex) { if (ex.Response != null) { HttpWebResponse errorResponse = (HttpWebResponse)ex.Response; StreamReader reader = new StreamReader(errorResponse.GetResponseStream()); string output = reader.ReadToEnd(); JObject jsonStr = JObject.Parse(output); return jsonStr; } else { return null; } } }

 

 

涉及项目的原因,代码只能提供这么多了,仅供参考

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • CSLight研究院之学习笔记脚本NGUI里的回调方法(二)

    CSLight研究院之学习笔记脚本NGUI里的回调方法(二)

    2018-06-20 08:08

  • 适用于PHP初学者的学习线路和建议

    适用于PHP初学者的学习线路和建议

    2018-06-20 08:08

  • 我的NodeJs学习小结(一)

    我的NodeJs学习小结(一)

    2018-04-24 08:01

  • Javascript学习笔记(JSON对象)

    Javascript学习笔记(JSON对象)

    2018-04-23 14:03

网友点评
>