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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Security;
  6. using System.Net;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.FromERP
  11. {
  12. public class RestOpHelper
  13. {
  14. private string url = "";
  15. public string Url
  16. {
  17. get { return url; }
  18. set { url = value; }
  19. }
  20. private string clientp12path = "";
  21. public string Clientp12path
  22. {
  23. get { return clientp12path; }
  24. set { clientp12path = value; }
  25. }
  26. private string clientp12PassWord = "";
  27. public string Clientp12PassWord
  28. {
  29. get { return clientp12PassWord; }
  30. set { clientp12PassWord = value; }
  31. }
  32. public RestOpHelper()
  33. {
  34. }
  35. public string send(string s)
  36. {
  37. string retrunstr = "";
  38. //类似浏览器确认证书合法方法的绑定
  39. //如果觉得写一个委托方法麻烦,可以直接使用匿名委托
  40. ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidate;
  41. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  42. Uri uri = new Uri(@url);
  43. HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
  44. byte[] bs = Encoding.UTF8.GetBytes(s);
  45. //这2句代码表示如果要求客户端证书,将客户端证书加入request,不需要客户端证书的https请求则不需要此代码 //需导入xx.p12证书文件 clientp12path为文件所在路径clientp12password为证书密码 InstallCertificate(clientp12path, clientp12PassWord, StoreLocation.CurrentUser, StoreName.My);
  46. X509Certificate cer = new X509Certificate(clientp12path, clientp12PassWord);
  47. request.Credentials = new NetworkCredential("IF_YERP", "Kfiw282082");
  48. request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
  49. //request.
  50. request.Timeout = 10000;
  51. request.ClientCertificates.Add(cer);
  52. request.ContentType = "text/plain";
  53. request.Method = "post";
  54. request.KeepAlive = false;
  55. request.ProtocolVersion = HttpVersion.Version10;
  56. request.Proxy = null;
  57. //request.UserAgent = DefaultUserAgent;
  58. using (Stream reqStram = request.GetRequestStream())
  59. {
  60. reqStram.Write(bs, 0, bs.Length);
  61. reqStram.Close();
  62. }
  63. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  64. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  65. {
  66. retrunstr = reader.ReadToEnd();
  67. }
  68. return retrunstr;
  69. }
  70. private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
  71. {
  72. // trust any certificate!!!
  73. //System.Console.WriteLine("Warning, trust any certificate");
  74. //为了通过证书验证,总是返回true
  75. return true;
  76. }
  77. //导入证书
  78. public static bool InstallCertificate(string certFilePath, string password, StoreLocation location, StoreName storeName)
  79. {
  80. try
  81. {
  82. if (!File.Exists(certFilePath))
  83. {
  84. return false;
  85. }
  86. byte[] certData = File.ReadAllBytes(certFilePath);
  87. X509Certificate2 cert = new X509Certificate2(certData, password, X509KeyStorageFlags.Exportable);
  88. X509Store store = new X509Store(storeName, location);
  89. store.Open(OpenFlags.MaxAllowed);
  90. store.Remove(cert);
  91. store.Add(cert);
  92. store.Close();
  93. return true;
  94. }
  95. catch (Exception ex)
  96. {
  97. return false;
  98. }
  99. }
  100. public void RequestSAP(string url, int timeout, string xmlData, string userName, string password, out string statusCode, out string resultContent)
  101. {
  102. statusCode = "400";
  103. resultContent = "";
  104. try
  105. {
  106. byte[] bytes = Encoding.UTF8.GetBytes(xmlData);
  107. //Basic Auth
  108. byte[] byteUser = Encoding.Default.GetBytes(userName + ":" + password);
  109. string Authorization = Convert.ToBase64String(byteUser);
  110. HttpWebRequest request = HttpWebRequest.CreateHttp(url);
  111. //这2句代码表示如果要求客户端证书,将客户端证书加入request,不需要客户端证书的https请求则不需要此代码 //需导入xx.p12证书文件 clientp12path为文件所在路径clientp12password为证书密码 InstallCertificate(clientp12path, clientp12PassWord, StoreLocation.CurrentUser, StoreName.My);
  112. X509Certificate cer = new X509Certificate(clientp12path, clientp12PassWord);
  113. request.ClientCertificates.Add(cer);
  114. //避免远程连接证书无效问题
  115. ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, errs) => true;
  116. request.Method = "POST";
  117. request.Timeout = timeout;
  118. request.ContentType = "text/xml;charset=UTF-8";
  119. request.ContentLength = bytes.Length;
  120. request.Headers.Add("Authorization", "Basic " + Authorization);
  121. //根据soapui中的SOAPAction 进行赋值
  122. request.Headers.Add("SOAPAction", "SOAPAction");
  123. Stream requestStream = request.GetRequestStream();
  124. requestStream.Write(bytes, 0, bytes.Length);
  125. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  126. statusCode = response.StatusCode.ToString("d");
  127. Stream responseStream = response.GetResponseStream();
  128. StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
  129. resultContent = sr.ReadToEnd();
  130. sr.Dispose();
  131. requestStream.Close();
  132. responseStream.Close();
  133. response.Dispose();
  134. }
  135. catch (Exception ex)
  136. {
  137. statusCode = "400";
  138. resultContent = ex.Message;
  139. }
  140. }
  141. }
  142. }