public Task <QQHttpResponse> ExecuteHttpRequestAsync(QQHttpRequest request, IQQHttpListener listener, CancellationToken token) { try { #if DEBUG if (request.RawUrl == QQConstants.URL_ROBOT_TULING) { //var str = httpItem.GetRequestHeader(); //var count = str.Length; } #endif var httpRequest = GetHttpRequest(request); var result1 = _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token); result1.Wait(); var result = result1.Result; #if DEBUG if (request.RawUrl == QQConstants.URL_ROBOT_TULING) { var str = request.GetRequestHeader(_cc); var count = str.Length; } #endif if (!result.IsSuccessStatusCode) { throw new QQException(QQErrorCode.ErrorHttpStatus, result.StatusCode.ToString()); } var response = new QQHttpResponse { ResponseMessage = result.ReasonPhrase, ResponseCode = (int)result.StatusCode, Headers = new Dictionary <string, List <string> >(), ResultType = request.ResultType, }; switch (request.ResultType) { case ResponseResultType.String: { var r = result.Content.ReadAsStringAsync(); r.Wait(); response.ResponseString = r.Result; // await result.Content.ReadAsStringAsync().ConfigureAwait(false); break; } case ResponseResultType.Byte: { var r = result.Content.ReadAsByteArrayAsync(); r.Wait(); response.ResponseBytes = r.Result; // await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false); break; } case ResponseResultType.Stream: { var r = result.Content.ReadAsByteArrayAsync(); r.Wait(); response.ResponseStream = new MemoryStream(r.Result); //await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false) break; } } foreach (var header in result.Headers) { response.Headers[header.Key] = header.Value.ToList(); } foreach (var header in result.Content.Headers) { response.Headers[header.Key] = header.Value.ToList(); } if (result.Headers.Location != null) { request.Url = result.Headers.Location.AbsoluteUri; //return ExecuteHttpRequest(request, listener); var task = Task.Factory.StartNew(() => { return(response); }); return(task); } else { listener.OnHttpHeader(response); listener.OnHttpRead(0, response.GetContentLength()); listener.OnHttpFinish(response); var task = Task.Factory.StartNew(() => { return(response); }); return(task); } } catch (Exception ex) { var qqEx = ex as QQException ?? new QQException(ex); // throw qqEx; // 不抛出,而是交给listener处理 listener.OnHttpError(qqEx); // 这个listener负责推送一个类型为ON_HTTP_ERROR的actor到线程池,这个actor会执行action的OnHttpError方法 return(null); } }