public static async Task <HttpClientResult <T> > SendRequestAsync <T>( this HttpClient httpClient, ClientConfig clientConfig, HttpMethod httpMethod, string path = "/", object param = null, object data = null, CurrentProcess process = null) { var response = await httpClient.CallGetResponse(clientConfig, httpMethod, path, param, data, process); if (response != null) { var result = JsonConvert.DeserializeObject <T>(response.Item2); if (result is ResponseJsonModel) { var obj = result as ResponseJsonModel; if (obj?.error?.code != null) { obj.error.message = "Lỗi"; } } return(HttpClientResult <T> .Create(response.Item1, result, response.Item3, response.Item4)); } else { return(HttpClientResult <T> .Create(response.Item1, TypeExtensions.GetDefaultValue <T>(), null, false)); } }
public IActionResult ToResponse <T>(HttpClientResult <ResponseJsonModel <T> > result) where T : class { if (result.StatusCode == System.Net.HttpStatusCode.OK) { return(Json(result.Data)); } return(StatusCode((int)HttpStatusCode.BadRequest)); }
public IActionResult ToResponse(HttpClientResult <ResponseActionJsonModel> result) { if (result.StatusCode == System.Net.HttpStatusCode.OK) { result.Data.success = true; return(Json(result.Data)); } return(StatusCode((int)HttpStatusCode.BadRequest)); }
/// <summary> /// Called to read the response asyncroneusly /// </summary> /// <param name="ar"></param> private void ReadResponse(IAsyncResult ar) { Stream stream = null; int bytesRead = 0; HttpClientResult result = HttpClientResult.Error; try { lock (_lock) { stream = (Stream)ar.AsyncState; bytesRead = stream.EndRead(ar); if (bytesRead > 0) { //add to the exiting data _dataBuilder.AddChunkReference(_buffer, bytesRead); //make a new chunk _buffer = new byte[MAX_BUFFER_SIZE]; //continue reading stream.BeginRead(_buffer, 0, MAX_BUFFER_SIZE, new AsyncCallback(ReadResponse), stream); } else { //construct the full response _response = _dataBuilder.ToArray(); result = HttpClientResult.Success; } } } catch { //we don't care that much for the errors that occur here } finally { if (bytesRead == 0) { //if the caller was waiting on the request complete event allow continuation _requestCompleteEvent.Set(); //we're done reading close the connection and trigger the event with the collected response //the result will be success if (RequestComplete != null) { RequestComplete.Invoke (new HttpClientRequestCompleteEventArgs(new HttpResponseInfo(_response))); } _connection.Close(); } } }
/// <summary> /// 设置登录成功后的登录状态 /// </summary> public void SetSourceLoginStatus(QuestSourceModel source, HttpClientResult result, LoginResultModel loginModel) { if (!VerifyHelper.IsEmpty(loginModel) && !VerifyHelper.IsEmpty(loginModel.obj) && loginModel.success) { var resultObj = loginModel.obj as JObject; var paramList = StringHelper.GetToListKeyValue(resultObj.GetString("returnUrl").Replace("?", ""), listSplit: '&'); if (!VerifyHelper.IsEmpty(paramList) && paramList.Where(x => x.Key.ToLower() == "uuid").Count() > 0) { source.Message = ""; source.RetryTotal = 0; source.Cookies = result.CookieCollection; source.IsLogin = true; source.UserId = paramList.Where(x => x.Key.ToLower() == "uuid").FirstOrDefault().Value; } } }
/// <summary> /// 获取验证码位图 /// </summary> /// <returns></returns> public static BitmapImage GetCaptchaBitmapImage(HttpClientResult result = null) { BitmapImage bmp = null; if (result == null) { result = GetCaptchResult(); } if (!string.IsNullOrWhiteSpace(result.Content)) { try { bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(result.Bytes); bmp.EndInit(); } catch { bmp = null; } } return(bmp); }
/// <summary> /// Constructor /// </summary> /// <param name="response">The result of an HttpClient send operation</param> /// <param name="result">The Http response, null if an error occurred</param> public HttpClientRequestCompleteEventArgs(byte [] response) { _result = HttpClientResult.Success; _byteResponse = response; }
/// <summary> /// Constructor /// </summary> /// <param name="response">The result of an HttpClient send operation</param> /// <param name="result">The Http response, null if an error occurred</param> public HttpClientRequestCompleteEventArgs(HttpResponseInfo response) { _result = HttpClientResult.Success; _httpResponse = response; }