设置协议Post调用
/// <summary> /// 发送数据 /// </summary> /// <param name="url">地址</param> /// <param name="postData">xml</param> /// <returns></returns> public static string DoPost(string url, string postData) { //这里已经url编码了一次 //string param = "logistics_interface =" + HttpUtility.UrlEncode(postData.Trim(), Encoding.GetEncoding("GBK")); //string param = HttpUtility.UrlEncode(postData.Trim(), Encoding.GetEncoding("UTF-8")); string param = postData.Trim(); byte[] bs = Encoding.GetEncoding("UTF-8").GetBytes(param); ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证
//解决 SSL/TLS 安全通道问题
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;
System.Net.ServicePointManager.Expect100Continue = false; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Timeout = 300000; request.Method = "POST"; request.ContentType = "text/json;application/x-www-form-urlencoded;charset=UTF-8;"; request.ContentLength = bs.Length; try { using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string responseString = sr.ReadToEnd(); sr.Close(); response.Close(); return responseString; } catch (Exception ex) { CommonCode.SaveExceptionLog("Post", "发送数据出现异常:" + ex.ToString()); throw ex; } }