接下来处理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; } } }
涉及项目的原因,代码只能提供这么多了,仅供参考