private static async Task <CommonRespMsg> SendRequest(HttpRequestMessage request, CancellationToken token) { var msgToReturn = new CommonRespMsg(); try { using (var client = new HttpClient()) { //client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Linux; U; Android 5.1; zh-cn; XT1085 Build/LPE23.32-53) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); HttpResponseMessage resp = null; if (token == null) { resp = await client.SendRequestAsync(request); } else { resp = await client.SendRequestAsync(request).AsTask(token); } resp.EnsureSuccessStatusCode(); var bytes = await resp.Content.ReadAsBufferAsync(); var content = Encoding.UTF8.GetString(bytes.ToArray()); msgToReturn.JsonSrc = content; JsonObject resultObj; var ok = JsonObject.TryParse(content, out resultObj); if (ok) { var errorCode = JsonParser.GetStringFromJsonObj(resultObj, "errorCode"); var errorMsg = JsonParser.GetStringFromJsonObj(resultObj, "errorMsg"); if (!string.IsNullOrEmpty(errorCode)) { if (errorCode != "0") { msgToReturn.IsSuccessful = false; } msgToReturn.ErrorCode = int.Parse(errorCode); msgToReturn.ErrorMsg = errorMsg; } } resp.Dispose(); } } catch (Exception e) { var task = ExceptionHelper.WriteRecordAsync(e, nameof(APIHelper), nameof(SendRequest) + request.RequestUri); msgToReturn.IsSuccessful = false; msgToReturn.ExtraErrorMsg += e.Message; //throw e; } return(msgToReturn); }
private static async Task<CommonRespMsg> SendRequest(HttpRequestMessage request,CancellationToken token) { var msgToReturn = new CommonRespMsg(); try { using (var client = new HttpClient()) { //client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Linux; U; Android 5.1; zh-cn; XT1085 Build/LPE23.32-53) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); HttpResponseMessage resp = null; if(token== null) { resp=await client.SendRequestAsync(request); } else resp = await client.SendRequestAsync(request).AsTask(token); resp.EnsureSuccessStatusCode(); var bytes = await resp.Content.ReadAsBufferAsync(); var content = Encoding.UTF8.GetString(bytes.ToArray()); msgToReturn.JsonSrc = content; JsonObject resultObj; var ok = JsonObject.TryParse(content, out resultObj); if (ok) { var errorCode = JsonParser.GetStringFromJsonObj(resultObj, "errorCode"); var errorMsg = JsonParser.GetStringFromJsonObj(resultObj, "errorMsg"); if (!string.IsNullOrEmpty(errorCode)) { if (errorCode != "0") { msgToReturn.IsSuccessful = false; } msgToReturn.ErrorCode = int.Parse(errorCode); msgToReturn.ErrorMsg = errorMsg; } } resp.Dispose(); } } catch(Exception e) { // var task = ExceptionHelper.WriteRecordAsync(e, nameof(APIHelper), nameof(SendRequest)+request.RequestUri); msgToReturn.IsSuccessful = false; msgToReturn.ExtraErrorMsg += e.Message; //throw e; } return msgToReturn; }