/// <summary> /// 创建http异步请求 /// </summary> /// <param name="requestInfo">请求信息</param> public void CreateAsyncHttpRequest(HttpRequestInfo requestInfo) { Info("HttpRequest[{0}]: {1}", retryCount, requestInfo); // ignore SSL certificate errors ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true; paramBytes = Encoding.UTF8.GetBytes(requestInfo.Param); HttpWebRequest request = HttpWebRequest.Create(requestInfo.URL) as HttpWebRequest; request.Method = requestInfo.Method; request.ContentType = "application/json"; request.ContentLength = paramBytes.Length; foreach (var element in requestInfo.Headers) { request.Headers.Add(element.Key, element.Value); } request.Timeout = 5000; request.KeepAlive = false; if (requestInfo.Method == "POST") { request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); } else { request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); } }
private void RetryRequest(HttpRequestInfo requestInfo, HttpStatusCode httpStatusCode, string error) { retryCount = retryCount + 1; if (retryCount > 3) { Info("HttpResponse[{0}]: {1},{2},{3}", retryCount - 1, "false", httpStatusCode, error); retryCount = 1; httpRequestCallback.Invoke(false, httpStatusCode, error); } else { CreateHttpRequest(requestInfo); } }
/// <summary> /// 创建http同步请求 /// </summary> /// <param name="requestInfo">请求信息</param> public void CreateHttpRequest(HttpRequestInfo requestInfo) { Info("HttpRequest[{0}]: {1}", retryCount, requestInfo); // ignore SSL certificate errors ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true; HttpWebRequest request = null; try { paramBytes = Encoding.UTF8.GetBytes(requestInfo.Param); request = HttpWebRequest.Create(requestInfo.URL) as HttpWebRequest; request.Method = requestInfo.Method; request.ContentType = "application/json"; request.ContentLength = paramBytes.Length; foreach (var element in requestInfo.Headers) { request.Headers.Add(element.Key, element.Value); } request.Timeout = 5000; request.KeepAlive = false; if (requestInfo.Method == "POST" && paramBytes.Length > 0) { using (Stream writer = request.GetRequestStream()) { writer.Write(paramBytes, 0, paramBytes.Length); } } } catch (Exception e) { Error("retryCount={0}, error={1}", retryCount, e.Message); request.Abort(); RetryRequest(requestInfo, HttpStatusCode.BadRequest, e.Message); return; } HttpWebResponse response = null; try { response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string responseJson = reader.ReadToEnd(); if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK) { Info("HttpResponse[{0}]: {1},{2},{3}", retryCount, "true", response.StatusCode, responseJson); retryCount = 1; httpRequestCallback.Invoke(true, response.StatusCode, responseJson); } else { Info("retryCount={0}, status={1}, response={2}", retryCount, response.StatusCode, responseJson); request.Abort(); RetryRequest(requestInfo, response.StatusCode, responseJson); } reader.Close(); response.Close(); } catch (Exception e) { Error("HttpResponse[{0}]: {1},{2},{3}", "false", retryCount, HttpStatusCode.ExpectationFailed, e.Message); retryCount = 1; request.Abort(); httpRequestCallback.Invoke(false, HttpStatusCode.ExpectationFailed, e.Message); } }