• WebService SOAP1.1 SOAP1.12 HTTP PSOT方式调用


    Visual Studio 2022 新建WebService项目

    创建之后启动运行

    设置默认文档即可

    经过上面的创建WebService已经创建完成,添加HelloWorld3方法,

    [WebMethod]
    public string HelloWorld3(int a, string b)
    {
    //var s = a + b;
    return $"Hello World a+b={a + b}";
    }

    属性页面如下:

     地址加上?wsdl----http://localhost:8012/WebService1.asmx?wsdl 可以查看具体方法,我们点开一个方法,查看具体调用方式,

    http://localhost:8012/WebService1.asmx?op=HelloWorld3

    下面使用 SOAP1.1 SOAP1.12 HTTP PSOT方式调用WebService,代码如下

    1. #region 测试 SOAP1.1 SOAP1.12 HTTP PSOT方式调用WebService 调用
    2. ///
    3. /// WebService SOAP1.1方法调用
    4. ///
    5. /// 调用方法所需参数
    6. public static string WebServiceSOAP11(int a, string b)
    7. {
    8. //http://localhost:8012/WebService1.asmx/HelloWorld3
    9. #region HTTP POST 请求和响应示例
    10. #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
    11. //SOAP 1.1
    12. //以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
    13. //请求
    14. //POST /WebService1.asmx HTTP/1.1
    15. //Host: localhost
    16. //Content-Type: text/xml; charset=utf-8
    17. //Content-Length: length替换
    18. //SOAPAction: "http://tempuri.org/HelloWorld3"
    19. //
    20. //
    21. //
    22. //
    23. // string替换
    24. //
    25. //
    26. //
    27. //响应
    28. //HTTP/1.1 200 OK
    29. //Content-Type: text/xml; charset=utf-8
    30. //Content-Length: length
    31. //
    32. //
    33. //
    34. //
    35. // string
    36. //
    37. //
    38. //
    39. #endregion
    40. HttpWebRequest httpWebRequest = null;
    41. string result = null;
    42. var webserviceurl = "http://localhost:8012/WebService1.asmx" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
    43. httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
    44. //注意SOAP1.1 ContentType,需要SOAPAction,Content-Type: text/xml; charset=utf-8
    45. httpWebRequest.ContentType = "text/xml; charset=utf-8";
    46. httpWebRequest.Method = "post";
    47. httpWebRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld3");
    48. Stream requestStream = httpWebRequest.GetRequestStream();
    49. StreamWriter streamWriter = new StreamWriter(requestStream);
    50. streamWriter.Write($"\r\n\r\n\r\n\r\n{a}\r\n{b}\r\n\r\n\r\n");
    51. streamWriter.Close();
    52. requestStream.Close();
    53. //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
    54. //requestStream.Write(vs, 0, vs.Length);
    55. httpWebRequest.ContentLength = vs.Length;
    56. //requestStream.Close();
    57. Stream responseStream = null;
    58. StreamReader reader = null;
    59. HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    60. try
    61. {
    62. if (webResponse.StatusCode == HttpStatusCode.OK)
    63. {
    64. //返回值类型 Content-Type: text/xml; charset=utf-8
    65. //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
    66. responseStream = webResponse.GetResponseStream();
    67. reader = new StreamReader(responseStream);
    68. result = reader.ReadToEnd();
    69. XmlDocument xmlDocument = new XmlDocument();
    70. xmlDocument.LoadXml(result);
    71. result = xmlDocument.InnerText;
    72. }
    73. }
    74. catch (Exception ex)
    75. {
    76. result = $"查询出错,原因:{ex}";
    77. }
    78. finally
    79. {
    80. reader.Close();
    81. webResponse.Close();
    82. responseStream.Close();
    83. httpWebRequest.Abort();
    84. }
    85. return result;
    86. //if (!string.IsNullOrEmpty(result))
    87. //{
    88. // System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
    89. //}
    90. #endregion
    91. }
    92. ///
    93. /// WebService SOAP1.2方法调用
    94. ///
    95. /// 调用方法所需参数
    96. public static string WebServiceSOAP12(int a, string b)
    97. {
    98. //http://localhost:8012/WebService1.asmx/HelloWorld3
    99. #region HTTP POST 请求和响应示例
    100. #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
    101. //SOAP 1.2
    102. //以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
    103. //POST /WebService1.asmx HTTP/1.1
    104. //Host: localhost
    105. //Content-Type: application/soap+xml; charset=utf-8
    106. //Content-Length: length
    107. //
    108. //
    109. //
    110. //
    111. // int
    112. // string
    113. //
    114. //
    115. //
    116. //HTTP/1.1 200 OK
    117. //Content-Type: application/soap+xml; charset=utf-8
    118. //Content-Length: length
    119. //
    120. //
    121. //
    122. //
    123. // string
    124. //
    125. //
    126. //
    127. #endregion
    128. HttpWebRequest httpWebRequest = null;
    129. string result = null;
    130. var webserviceurl = "http://localhost:8012/WebService1.asmx" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
    131. httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
    132. //注意与SOAP1.1 区分 ContentType,不需要SOAPAction,Content-Type: application/soap+xml; charset=utf-8
    133. httpWebRequest.ContentType = "application/soap+xml; charset=utf-8";
    134. httpWebRequest.Method = "post";
    135. //不需要了 httpWebRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld3");
    136. Stream requestStream = httpWebRequest.GetRequestStream();
    137. StreamWriter streamWriter = new StreamWriter(requestStream);
    138. streamWriter.Write($"\r\n\r\n\r\n\r\n{a}\r\n{b}\r\n\r\n\r\n");
    139. streamWriter.Close();
    140. requestStream.Close();
    141. //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
    142. //requestStream.Write(vs, 0, vs.Length);
    143. httpWebRequest.ContentLength = vs.Length;
    144. //requestStream.Close();
    145. Stream responseStream = null;
    146. StreamReader reader = null;
    147. HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    148. try
    149. {
    150. if (webResponse.StatusCode == HttpStatusCode.OK)
    151. {
    152. //返回值类型 Content-Type: application/soap+xml; charset=utf-8
    153. //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
    154. responseStream = webResponse.GetResponseStream();
    155. reader = new StreamReader(responseStream);
    156. result = reader.ReadToEnd();
    157. XmlDocument xmlDocument = new XmlDocument();
    158. xmlDocument.LoadXml(result);
    159. result = xmlDocument.InnerText;
    160. }
    161. }
    162. catch (Exception ex)
    163. {
    164. result = $"查询出错,原因:{ex}";
    165. }
    166. finally
    167. {
    168. reader.Close();
    169. webResponse.Close();
    170. responseStream.Close();
    171. httpWebRequest.Abort();
    172. }
    173. return result;
    174. //if (!string.IsNullOrEmpty(result))
    175. //{
    176. // System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
    177. //}
    178. #endregion
    179. }
    180. ///
    181. /// WebService HTTP方法调用
    182. ///
    183. /// 调用方法所需参数
    184. public static string WebServiceHTTP(string xmldata)
    185. {
    186. //http://localhost:8012/WebService1.asmx/HelloWorld3
    187. #region HTTP POST 请求和响应示例
    188. #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
    189. //以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。
    190. // POST /WebService1.asmx/HelloWorld3 HTTP/1.1
    191. // Host: localhost
    192. // Content-Type: application/x-www-form-urlencoded
    193. // Content-Length: length替换
    194. // a=string替换&b=string替换
    195. // HTTP/1.1 200 OK
    196. // Content-Type: text/xml; charset=utf-8
    197. // Content-Length: length
    198. //
    199. // string
    200. #endregion
    201. HttpWebRequest httpWebRequest = null;
    202. string result = null;
    203. var webserviceurl = "http://localhost:8012/WebService1.asmx/HelloWorld3" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
    204. httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
    205. //注意与SOAP1.1,SOAP1.2 区分 ContentType,不需要SOAPAction,Content-Type: application/x-www-form-urlencoded
    206. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    207. httpWebRequest.Method = "post";
    208. Stream requestStream = httpWebRequest.GetRequestStream();
    209. StreamWriter streamWriter = new StreamWriter(requestStream);
    210. streamWriter.Write(xmldata);
    211. streamWriter.Close();
    212. requestStream.Close();
    213. //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
    214. //requestStream.Write(vs, 0, vs.Length);
    215. httpWebRequest.ContentLength = vs.Length;
    216. //requestStream.Close();
    217. Stream responseStream = null;
    218. StreamReader reader = null;
    219. HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    220. try
    221. {
    222. if (webResponse.StatusCode == HttpStatusCode.OK)
    223. {
    224. //返回值类型 Content-Type: text/xml; charset=utf-8
    225. //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
    226. responseStream = webResponse.GetResponseStream();
    227. reader = new StreamReader(responseStream);
    228. result = reader.ReadToEnd();
    229. XmlDocument xmlDocument = new XmlDocument();
    230. xmlDocument.LoadXml(result);
    231. result = xmlDocument.InnerText;
    232. }
    233. }
    234. catch (Exception ex)
    235. {
    236. result = $"查询出错,原因:{ex}";
    237. }
    238. finally
    239. {
    240. reader.Close();
    241. webResponse.Close();
    242. responseStream.Close();
    243. httpWebRequest.Abort();
    244. }
    245. return result;
    246. //if (!string.IsNullOrEmpty(result))
    247. //{
    248. // System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
    249. //}
    250. #endregion
    251. }
    252. #endregion

    使用代码

    1. string aa = WebServiceSOAP11(4, "888");
    2. Console.WriteLine($"WebService--SOAP1.1-- 返回值:{aa}");
    3. aa = WebServiceSOAP11(6, "0000");
    4. Console.WriteLine($"WebService--SOAP1.2-- 返回值:{aa}");
    5. aa = WebServiceHTTP("a=666666&b=8888");//注意参数名称不一致会报错,a,b
    6. Console.WriteLine($"WebService--http-- 返回值:{aa}");

    运行效果

  • 相关阅读:
    Python数据分析与机器学习51-EDA之粮农组织数据
    高并发下的分布式缓存 | Read-Through缓存模式
    Gif图片分解(支持批量)
    pgpool-II 4.3 中文手册 - 入门教程
    腾讯云GPU云服务器计算型GN7有哪些特点?适用于哪些场景?
    .NET进程守护服务
    面试突击91:MD5 加密安全吗?
    Idea如何上传项目至GitHub
    半导体行业如何在跨网数据交换时保证核心数据是安全的?
    工程伦理与学术道德
  • 原文地址:https://blog.csdn.net/LongtengGensSupreme/article/details/133998583