public List <V> HttpClientDoPost <T, V>(string baseUrl, string ticket, string requestMethod, T model, out string resultMsg) //where V : new() { resultMsg = ""; if (string.IsNullOrEmpty(baseUrl)) { baseUrl = ComClass.GetWebBaseUrl(); } if (string.IsNullOrEmpty(ticket)) { // ticket = BasicAuthenticationAttribute.webApiAccessKey; //本网站使用,不必放在配置文件中 } var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; // var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None }; try { using (var httpclient = new HttpClient(handler)) { httpclient.BaseAddress = new Uri(baseUrl); httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ticket); // httpclient.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=UTF-8"); Dictionary <string, object> dic = GetPropertity(model); var content = new FormUrlEncodedContent(dic.ToDictionary(k => k.Key, v => v.Value != null ? v.Value.ToString() : "")); var response = httpclient.PostAsync(requestMethod, content).Result; if (response.IsSuccessStatusCode) { var responseString = response.Content.ReadAsStringAsync(); var resultT = GetListFromJsonString <V>(responseString.Result.ToString(), out resultMsg); // V resultT = Common.SerializerHelper.DeserializeToObject<V>(responseString); return(resultT); } else { int code = (int)response.StatusCode; resultMsg = "执行失败code=" + code; // return default(V); return(new List <V>()); // return resultT response.ReasonPhrase; } } } catch (Exception ex) { Log2Net.LogApi.WriteExceptLog(ex, "WebApiHelper.HttpClientDoPost"); resultMsg = ex.Message; // return default(V); return(new List <V>()); } }
string HttpClientDoGet <T>(string baseUrl, string ticket, string requestMethod, out string resultMsg) { resultMsg = ""; if (string.IsNullOrEmpty(baseUrl)) { baseUrl = ComClass.GetWebBaseUrl(); } if (string.IsNullOrEmpty(ticket)) { // ticket = BasicAuthenticationAttribute.webApiAccessKey;//本网站使用时,不必放在配置文件中 } var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; try { using (HttpClient client = new HttpClient(handler)) { client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ticket); HttpResponseMessage response = client.GetAsync(requestMethod).Result; if (response.IsSuccessStatusCode) { var resultStr = response.Content.ReadAsStringAsync();//返回的是json字符串,可反序列化得到对象 // IEnumerable<T> resultModel = response.Content.ReadAsAsync<IEnumerable<T>>().Result; resultMsg = "OK"; return(resultStr.Result); } else { int code = (int)response.StatusCode; string msg = response.ReasonPhrase; resultMsg = msg; return(SerializeHelper.SerializeToString(new List <T>())); } } } catch (Exception ex) { Log2Net.LogApi.WriteExceptLog(ex, "WebApiHelper.HttpClientDoGet"); resultMsg = ex.Message; return(SerializeHelper.SerializeToString(new List <T>())); } }