/// <summary> /// 这里用同步的方式,因为我们是在定时器线程中调用的,不影响UI线程 /// </summary> /// <param name="list"></param> /// <param name="url"></param> /// <returns></returns> public BllResult FormPost(List <KeyValuePair <string, string> > list, WMSUrls url, HttpClient client, List <DictDetail> urls) { try { client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var ret = client.PostAsync(urls.First(t => t.Code == url.ToString()).Value, GetFormUrlEncodedContent(list)).Result; if (ret.IsSuccessStatusCode) { ret.EnsureSuccessStatusCode(); string temp = ret.Content.ReadAsStringAsync().Result; AppSession.LogService.WriteInfoLog($"请求WMS接口{url.ToString()}成功,返回值:{temp}"); var b = JsonConvert.DeserializeObject <ResponseModel <Object> >(temp); if (b.Code == "200") { return(BllResultFactory.Sucess(null, b.Msg)); } else { return(BllResultFactory.Error(null, b.Msg)); } } else { return(BllResultFactory.Error(null, $"请求WMS失败,请检查网络连接,详情:{ret.StatusCode}")); } } catch (Exception ex) { return(BllResultFactory.Error(null, $"请求WMS失败,请检查网络连接或者重启本程序,详情:{ex.ToString()}")); } }
/// <summary> /// 这里用异步的方式,UI线程中请使用这个方法 /// </summary> /// <param name="list"></param> /// <param name="url"></param> /// <returns></returns> public async Task <BllResult> FormPostAsync(List <KeyValuePair <string, string> > list, WMSUrls url, HttpClient client, List <DictDetail> urls) { try { var ret = await client.PostAsync(urls.First(t => t.Code == url.ToString()).Value, GetFormUrlEncodedContent(list)); if (ret.IsSuccessStatusCode) { ret.EnsureSuccessStatusCode(); string temp = ret.Content.ReadAsStringAsync().Result; var b = JsonConvert.DeserializeObject <ResponseModel <Object> >(temp); if (b.Code == "200") { return(BllResultFactory.Sucess(null, b.Msg)); } else { return(BllResultFactory.Error(null, b.Msg)); } } else { return(BllResultFactory.Error(null, "请求WMS失败,请检查网络连接")); } } catch (Exception ex) { return(BllResultFactory.Error(null, "请求WMS失败,请检查网络连接或者重启本程序")); } }