151 lines
6.0 KiB
C#
151 lines
6.0 KiB
C#
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方式添加数据(传输)
|
|
/// <summary>
|
|
/// 通过Json方式添加数据
|
|
/// Mr.Cai 2018-1-3
|
|
/// </summary>
|
|
/// <param name="ipAddress">完整请求地址</param>
|
|
/// <param name="interfaceName">接口名字</param>
|
|
/// <param name="tableName">表名</param>
|
|
/// <param name="code">授权码</param>
|
|
/// <param name="jsonString">json参数数组</param>
|
|
/// <param name="orderString">其它参数</param>
|
|
/// <returns></returns>
|
|
|
|
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请求
|
|
/// <summary>
|
|
/// Post请求
|
|
/// </summary>
|
|
/// <param name="requestUrl">请求地址</param>
|
|
/// <param name="postString">请求参数</param>
|
|
/// <param name="contentType">请求头</param>
|
|
/// <param name="timeOut">请求超时值</param>
|
|
/// <returns></returns>
|
|
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方式添加数据(对外)
|
|
///// <summary>
|
|
///// 通过Json方式添加数据
|
|
///// Mr.Cai 2018-1-3
|
|
///// </summary>
|
|
///// <param name="ipAddress">请求地址</param>
|
|
///// <param name="interfaceName">接口名字</param>
|
|
///// <param name="code">授权码</param>
|
|
///// <param name="jsonString">json参数数组</param>
|
|
///// <returns></returns>
|
|
|
|
//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
|
|
}
|
|
}
|