IcsFromERPJob
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.5 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ICSSoft.FromERP
{
public class RestOpHelper
{
private string url = "";
public string Url
{
get { return url; }
set { url = value; }
}
private string clientp12path = "";
public string Clientp12path
{
get { return clientp12path; }
set { clientp12path = value; }
}
private string clientp12PassWord = "";
public string Clientp12PassWord
{
get { return clientp12PassWord; }
set { clientp12PassWord = value; }
}
public RestOpHelper()
{
}
public string send(string s)
{
string retrunstr = "";
//类似浏览器确认证书合法方法的绑定
//如果觉得写一个委托方法麻烦,可以直接使用匿名委托
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Uri uri = new Uri(@url);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
byte[] bs = Encoding.UTF8.GetBytes(s);
//这2句代码表示如果要求客户端证书,将客户端证书加入request,不需要客户端证书的https请求则不需要此代码 //需导入xx.p12证书文件 clientp12path为文件所在路径clientp12password为证书密码 InstallCertificate(clientp12path, clientp12PassWord, StoreLocation.CurrentUser, StoreName.My);
X509Certificate cer = new X509Certificate(clientp12path, clientp12PassWord);
request.Credentials = new NetworkCredential("IF_YERP", "Xtxc355860");
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
//request.
request.Timeout = 10000;
request.ClientCertificates.Add(cer);
request.ContentType = "text/plain";
request.Method = "post";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Proxy = null;
//request.UserAgent = DefaultUserAgent;
using (Stream reqStram = request.GetRequestStream())
{
reqStram.Write(bs, 0, bs.Length);
reqStram.Close();
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
retrunstr = reader.ReadToEnd();
}
return retrunstr;
}
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
// trust any certificate!!!
//System.Console.WriteLine("Warning, trust any certificate");
//为了通过证书验证,总是返回true
return true;
}
//导入证书
public static bool InstallCertificate(string certFilePath, string password, StoreLocation location, StoreName storeName)
{
try
{
if (!File.Exists(certFilePath))
{
return false;
}
byte[] certData = File.ReadAllBytes(certFilePath);
X509Certificate2 cert = new X509Certificate2(certData, password, X509KeyStorageFlags.Exportable);
X509Store store = new X509Store(storeName, location);
store.Open(OpenFlags.MaxAllowed);
store.Remove(cert);
store.Add(cert);
store.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public void RequestSAP(string url, int timeout, string xmlData, string userName, string password, out string statusCode, out string resultContent)
{
statusCode = "400";
resultContent = "";
try
{
byte[] bytes = Encoding.UTF8.GetBytes(xmlData);
//Basic Auth
byte[] byteUser = Encoding.Default.GetBytes(userName + ":" + password);
string Authorization = Convert.ToBase64String(byteUser);
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
//这2句代码表示如果要求客户端证书,将客户端证书加入request,不需要客户端证书的https请求则不需要此代码 //需导入xx.p12证书文件 clientp12path为文件所在路径clientp12password为证书密码 InstallCertificate(clientp12path, clientp12PassWord, StoreLocation.CurrentUser, StoreName.My);
X509Certificate cer = new X509Certificate(clientp12path, clientp12PassWord);
request.ClientCertificates.Add(cer);
//避免远程连接证书无效问题
ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, errs) => true;
request.Method = "POST";
request.Timeout = timeout;
request.ContentType = "text/xml;charset=UTF-8";
request.ContentLength = bytes.Length;
request.Headers.Add("Authorization", "Basic " + Authorization);
//根据soapui中的SOAPAction 进行赋值
request.Headers.Add("SOAPAction", "SOAPAction");
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
statusCode = response.StatusCode.ToString("d");
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
resultContent = sr.ReadToEnd();
sr.Dispose();
requestStream.Close();
responseStream.Close();
response.Dispose();
}
catch (Exception ex)
{
statusCode = "400";
resultContent = ex.Message;
}
}
}
}