|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Net.Http;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Web;
|
|
|
|
|
|
namespace POSV.OtherMember.Utils
|
|
|
{
|
|
|
public class MemberHttpUtils
|
|
|
{
|
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
private static int TIMEOUT = 30 * 1000;
|
|
|
|
|
|
|
|
|
private static HttpClient GetHttpClient()
|
|
|
{
|
|
|
HttpClient httpClient = null;
|
|
|
|
|
|
|
|
|
var handler = new HttpClientHandler();
|
|
|
if (handler.SupportsAutomaticDecompression)
|
|
|
{
|
|
|
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
|
|
}
|
|
|
|
|
|
handler.Proxy = null;
|
|
|
handler.UseDefaultCredentials = false;
|
|
|
|
|
|
Interlocked.CompareExchange(ref httpClient, new HttpClient(handler) { BaseAddress = new Uri(MemberApiUtils.URL) }, null);
|
|
|
|
|
|
httpClient.DefaultRequestHeaders.Connection.Add("Keep-Alive");
|
|
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
|
|
|
//_httpClient.DefaultRequestHeaders.Add("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
|
|
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); //声明给服务器,客户端支持gzip解压
|
|
|
httpClient.DefaultRequestHeaders.Add("Accept-Language", "zh-cn,en-us;q=0.7,en;q=0.3");
|
|
|
httpClient.DefaultRequestHeaders.Add("Pragma", "no-cache");
|
|
|
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
|
|
|
|
|
|
httpClient.DefaultRequestHeaders.Add("JW-API-VERSION", "1.0.0");
|
|
|
|
|
|
//zhangy 2019-11-24 10:13
|
|
|
httpClient.MaxResponseContentBufferSize = int.MaxValue;
|
|
|
httpClient.DefaultRequestHeaders.ExpectContinue = false;
|
|
|
httpClient.Timeout = TimeSpan.FromMilliseconds(TIMEOUT);
|
|
|
|
|
|
return httpClient;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static string PostAsync(SortedList<string, string> parameters)
|
|
|
{
|
|
|
Stopwatch watch = Stopwatch.StartNew();
|
|
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
//提升系统外联的最大并发web访问数
|
|
|
ServicePointManager.DefaultConnectionLimit = 512;
|
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
|
|
|
var stringParams = MemberJsonUtils.Serialize(parameters);
|
|
|
|
|
|
logger.Info("请求参数:{0}", stringParams);
|
|
|
|
|
|
|
|
|
Console.WriteLine("请求参数:" + stringParams);
|
|
|
|
|
|
|
|
|
var httpClient = GetHttpClient();
|
|
|
|
|
|
var content = new StringContent(stringParams, Encoding.UTF8, "application/json");
|
|
|
|
|
|
var task = httpClient.PostAsync(MemberApiUtils.URL, content);
|
|
|
|
|
|
task.Result.EnsureSuccessStatusCode();
|
|
|
|
|
|
HttpResponseMessage response = task.Result;
|
|
|
|
|
|
var result = response.Content.ReadAsStringAsync();
|
|
|
|
|
|
var responseDetails = result.Result;
|
|
|
|
|
|
|
|
|
Console.WriteLine("应答信息:" + responseDetails);
|
|
|
|
|
|
|
|
|
//logger.Info("Sucessfully sent HTTP request to api Invoices with repsonse" + responseDetails);
|
|
|
|
|
|
return responseDetails;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
string message = "调用PostAsync方法发生错误";
|
|
|
if (parameters != null && parameters.ContainsKey("method"))
|
|
|
{
|
|
|
message = "调用<" + parameters["method"] + ">发生错误";
|
|
|
}
|
|
|
logger.Error(ex, message);
|
|
|
|
|
|
throw new Exception(message);
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
string methodName = "NoDefined";
|
|
|
if (parameters != null && parameters.ContainsKey("method"))
|
|
|
{
|
|
|
methodName = parameters["method"];
|
|
|
}
|
|
|
logger.Info("调用<{0}>方法,耗时<{1}>毫秒", methodName, watch.ElapsedMilliseconds);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|