/// <summary> /// 初始化异步请求对象 /// </summary> /// <param name="request"> </param> /// <param name="callback"> </param> /// <param name="state"> </param> public HttpClientBeginResult(IHttpRequest request, AsyncCallback callback, object state) { _asyncCallback = callback; _timer = HttpRequestTimer.OnStart(); _request = request; request.OnInitialize(); _requestData = new HttpRequestData(request); _webRequest = Convert(_requestData); _timer.OnReady(); request.OnSending(); _asyncResult = _webRequest.BeginGetResponse(OnCallback, state); }
/// <summary> /// 同步发送 HTTP 请求 /// </summary> /// <param name="request"> 请求对象 </param> /// <returns> </returns> public IHttpResponse Send(IHttpRequest request) { var timer = HttpRequestTimer.OnStart(); var data = default(HttpRequestData); try { request.OnInitialize(); data = new HttpRequestData(request); request.Logger?.Write(TraceEventType.Verbose, () => data.Raw); var www = Convert(data); timer.OnReady(); request.OnSending(); var response = (HttpWebResponse)www.GetResponse(); timer.OnSend(); request.Response = Convert(response, request.CookieMode != HttpCookieMode.None); request.OnEnd(request.Response); } catch (Exception ex) { timer.OnError(); request.Logger?.Write(TraceEventType.Error, "请求中出现错误", ex); var wex = ex as WebException; if (wex != null) { var res = Convert((HttpWebResponse)wex.Response, request.CookieMode != HttpCookieMode.None); res.Exception = ex; request.Response = res; } else { var res = new HttpResponse { Exception = ex }; request.Response = res; } request.OnError(request.Response); } finally { timer.OnEnd(); request.Logger?.Write(TraceEventType.Verbose, () => request.Response?.ResponseRaw); request.Logger?.Write(TraceEventType.Information, timer.ToString()); } ((HttpResponse)request.Response).RequestData = data; return(request.Response); }
/// <summary> /// 发送异步请求 /// </summary> /// <param name="request"> 请求对象 </param> /// <param name="cancellationToken"> 用于取消请求的通知对象 </param> /// <returns> </returns> public async Task <IHttpResponse> SendAsync(IHttpRequest request, CancellationToken cancellationToken) { var timer = HttpRequestTimer.OnStart(); var data = default(HttpRequestData); try { request.OnInitialize(); data = new HttpRequestData(request); var www = Convert(data); request.Logger?.Write(TraceEventType.Verbose, () => data.Raw); timer.OnReady(); request.OnSending(); using (var source1 = new CancellationTokenSource(data.Timeout.TotalMilliseconds >= int.MaxValue ? int.MaxValue : (int)data.Timeout.TotalMilliseconds)) using (var source2 = CancellationTokenSource.CreateLinkedTokenSource(source1.Token, cancellationToken)) using (var client = new HttpClientWrapper(request)) //为了解决动态代理的问题 { var response = await client.SendAsync(www, source2.Token); var cookies = data.Cookies; while (request.AutoRedirect && response.StatusCode == HttpStatusCode.Redirect) //手动处理302的请求 { request.Logger?.Write(TraceEventType.Verbose, () => request.Response?.ResponseRaw); request.Logger?.Write(TraceEventType.Information, "StatusCode=302; 正在重定向..."); if (cookies == null) { cookies = new CookieContainer(); //302时必须使用 cookie } SetCookies(response, cookies); www = Convert(data, response.Headers.Location); //构建新的请求 request.Logger?.Write(TraceEventType.Verbose, () => data.Raw); response = await client.SendAsync(www, source2.Token); } timer.OnSend(); request.Response = await Convert(response, request.CookieMode != HttpCookieMode.None); SetCookies(response, cookies); request.OnEnd(request.Response); } } catch (Exception ex) { timer.OnError(); request.Logger?.Write(TraceEventType.Error, "异步请求中出现错误", ex); if (ex is TaskCanceledException) { ex = new TimeoutException("请求已超时"); } var res = new HttpResponse { Exception = ex }; request.Response = res; request.OnError(res); } finally { timer.OnEnd(); request.Logger?.Write(TraceEventType.Verbose, () => request.Response?.ResponseRaw); request.Logger?.Write(TraceEventType.Information, timer.ToString()); } ((HttpResponse)request.Response).RequestData = data; return(request.Response); }