using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace Transmission.SDK { public class HttpDataHelper { #region 方法 -> 通过Json方式添加数据(传输) /// /// 通过Json方式添加数据 /// Mr.Cai 2018-1-3 /// /// 完整请求地址 /// 接口名字 /// 表名 /// 授权码 /// json参数数组 /// 其它参数 /// public static string InsertDataByJson(string ipAddress, string interfaceName, string tableName, string code, string jsonString, string orderString = "") { if (string.IsNullOrEmpty(ipAddress) || string.IsNullOrEmpty(interfaceName) || string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(jsonString)) { throw new Exception("请求地址、接口名、表名或请求参数不可为空!"); } switch (tableName) { case "HIGHWAY_EXCHANGE.T_ENDACCOUNT_ZJ": tableName = "HIGHWAY_EXCHANGE.T_ENDACCOUNT_NEW"; break; case "HIGHWAY_EXCHANGE.T_PERSONSELL_ZJ": tableName = "HIGHWAY_EXCHANGE.T_PERSONSELL_NEW"; break; } try { //string[] args = { code, tableName, josnString, orderString }; //return WSHelper.InvokeWebService(ipAddress, interfaceName, args).ToString(); System.Collections.Hashtable hashtable = new System.Collections.Hashtable { { "code", code}, { "tableName" , tableName }, { "jsonString" , jsonString }, { "orderString" , orderString } }; return SoapWSHelper.QuerySoapWebServiceString(ipAddress, interfaceName, hashtable); } catch (Exception ex) { throw ex; } } #endregion #region 方法 -> Post请求 /// /// Post请求 /// /// 请求地址 /// 请求参数 /// 请求头 /// 请求超时值 /// public static string OnPost(string requestUrl, string postString, string contentType = "application/x-www-form-urlencoded", int timeOut = 0) { try { CookieContainer cookieContainer = new CookieContainer(); byte[] postBytes = Encoding.UTF8.GetBytes(postString); // 设置提交的相关参数 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; if (timeOut > 0) { request.Timeout = timeOut * 1000; } request.Method = "POST"; request.KeepAlive = false; request.ContentType = contentType; request.CookieContainer = cookieContainer; request.ContentLength = postBytes.Length; using (System.IO.Stream reqStream = request.GetRequestStream()) { //LogHelper.WriteSendLog("==开始写数据=="); reqStream.Write(postBytes, 0, postBytes.Length); //LogHelper.WriteSendLog("==写入完成=="); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { //在这里对接收到的页面内容进行处理 System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } } catch (Exception ex) { return ex.Message; } } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } #endregion #region 方法 -> 通过Json方式添加数据(对外) ///// ///// 通过Json方式添加数据 ///// Mr.Cai 2018-1-3 ///// ///// 请求地址 ///// 接口名字 ///// 授权码 ///// json参数数组 ///// //public static string InsertDataByJson(string ipAddress, string interfaceName, string code, string jsonString) //{ // if (string.IsNullOrEmpty(ipAddress) || string.IsNullOrEmpty(interfaceName) || string.IsNullOrEmpty(jsonString)) // { // return "请求地址、接口名或请求参数不可为空!"; // } // try // { // string[] args = { code, jsonString }; // string _ServiceUrl = string.Format("http://{0}/TableDataService/Service.asmx", ipAddress); // return WSHelper.InvokeWebService(_ServiceUrl, interfaceName, args).ToString(); // } // catch (Exception ex) // { // return "网络请求失败:" + ex.ToString(); // } //} #endregion } }