SOAPHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace WebAPIBase.Utils
  9. {
  10. public static class SOAPHelper
  11. {
  12. /// <summary>
  13. /// 发送SOAP请求,并返回响应xml
  14. /// </summary>
  15. /// <param name="url">请求地址http://www.wjg121.cn/Service/IPAddress.asmx?op=GetIPCountryAndLocal </param>
  16. /// <param name="datastr">SOAP请求信息 </param>
  17. /// <param name="action">请求类型 POST GET</param>
  18. /// <returns>返回响应信息</returns>
  19. public static string GetSOAPReSource(string url, string datastr, string action)
  20. {
  21. //发起请求
  22. Uri uri = new Uri(url);
  23. WebRequest webRequest = WebRequest.Create(uri);
  24. webRequest.ContentType = "text/xml; charset=utf-8";
  25. webRequest.Method = action;
  26. using (Stream requestStream = webRequest.GetRequestStream())
  27. {
  28. byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
  29. requestStream.Write(paramBytes, 0, paramBytes.Length);
  30. }
  31. //响应
  32. WebResponse webResponse = webRequest.GetResponse();
  33. using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
  34. {
  35. string result = "";
  36. return result = myStreamReader.ReadToEnd();
  37. }
  38. //var url = @"http://222.223.237.11:8189/WorkFlowWebService60NodeJS/Query.asmx";
  39. ////构造soap请求信息
  40. //StringBuilder soap = new StringBuilder();
  41. //soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  42. //soap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
  43. //soap.Append("<soap:Body>");
  44. //soap.Append("<Login xmlns=\"http://tempuri.org/\">");
  45. //soap.Append(string.Format("<userId>{0}</userId>", request.UserName));
  46. //soap.Append(string.Format("<password>{0}</password>", request.Password));
  47. //soap.Append(string.Format("<dNo>{0}</dNo>", ""));
  48. //soap.Append(string.Format("<dToken>{0}</dToken>", ""));
  49. //soap.Append("</Login>");
  50. //soap.Append("</soap:Body>");
  51. //soap.Append("</soap:Envelope>");
  52. //var result = SOAPHelper.GetSOAPReSource(url, soap.ToString(), "POST");
  53. }
  54. }
  55. }