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,代码如下

cs 复制代码
  #region 测试 SOAP1.1 SOAP1.12 HTTP PSOT方式调用WebService 调用
        /// <summary>
        /// WebService SOAP1.1方法调用
        /// </summary>
        /// <param name="xmldata">调用方法所需参数</param>        
        public static string WebServiceSOAP11(int a, string b)
        {
            //http://localhost:8012/WebService1.asmx/HelloWorld3
            #region HTTP POST 请求和响应示例
            #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
            //SOAP 1.1
            //以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
            //请求
            //POST /WebService1.asmx HTTP/1.1
            //Host: localhost
            //Content-Type: text/xml; charset=utf-8
            //Content-Length: length替换
            //SOAPAction: "http://tempuri.org/HelloWorld3"

            //<?xml version="1.0" encoding="utf-8"?>
            //<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/">
            //  <soap:Body>
            //    <HelloWorld3 xmlns="http://tempuri.org/">
            //      <a>int替换</a>
            //      <b>string替换</b>
            //    </HelloWorld3>
            //  </soap:Body>
            //</soap:Envelope>

            //响应
            //HTTP/1.1 200 OK
            //Content-Type: text/xml; charset=utf-8
            //Content-Length: length

            //<?xml version="1.0" encoding="utf-8"?>
            //<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/">
            //  <soap:Body>
            //    <HelloWorld3Response xmlns="http://tempuri.org/">
            //      <HelloWorld3Result>string</HelloWorld3Result>
            //    </HelloWorld3Response>
            //  </soap:Body>
            //</soap:Envelope>
            #endregion

            HttpWebRequest httpWebRequest = null;
            string result = null;
            var webserviceurl = "http://localhost:8012/WebService1.asmx" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
            httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
            //注意SOAP1.1 ContentType,需要SOAPAction,Content-Type: text/xml; charset=utf-8
            httpWebRequest.ContentType = "text/xml; charset=utf-8";
            httpWebRequest.Method = "post";
            httpWebRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld3");

            Stream requestStream = httpWebRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream);
            streamWriter.Write($"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<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/\">\r\n<soap:Body>\r\n<HelloWorld3 xmlns=\"http://tempuri.org/\">\r\n<a>{a}</a>\r\n<b>{b}</b>\r\n</HelloWorld3>\r\n</soap:Body>\r\n</soap:Envelope>");
            streamWriter.Close();
            requestStream.Close();

            //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
            //requestStream.Write(vs, 0, vs.Length);
            httpWebRequest.ContentLength = vs.Length;
            //requestStream.Close();

            Stream responseStream = null;
            StreamReader reader = null;
            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            try
            {
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    //返回值类型  Content-Type: text/xml; charset=utf-8
                    //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                    responseStream = webResponse.GetResponseStream();
                    reader = new StreamReader(responseStream);
                    result = reader.ReadToEnd();
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(result);
                    result = xmlDocument.InnerText;
                }
            }
            catch (Exception ex)
            {
                result = $"查询出错,原因:{ex}";
            }
            finally
            {
                reader.Close();
                webResponse.Close();
                responseStream.Close();
                httpWebRequest.Abort();
            }
            return result;
            //if (!string.IsNullOrEmpty(result))
            //{
            //    System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
            //}
            #endregion
        }

        /// <summary>
        /// WebService SOAP1.2方法调用
        /// </summary>
        /// <param name="xmldata">调用方法所需参数</param>        
        public static string WebServiceSOAP12(int a, string b)
        {
            //http://localhost:8012/WebService1.asmx/HelloWorld3
            #region HTTP POST 请求和响应示例
            #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
            //SOAP 1.2
            //以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

            //POST /WebService1.asmx HTTP/1.1
            //Host: localhost
            //Content-Type: application/soap+xml; charset=utf-8
            //Content-Length: length

            //<?xml version="1.0" encoding="utf-8"?>
            //<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
            //  <soap12:Body>
            //    <HelloWorld3 xmlns="http://tempuri.org/">
            //      <a>int</a>
            //      <b>string</b>
            //    </HelloWorld3>
            //  </soap12:Body>
            //</soap12:Envelope>
            //HTTP/1.1 200 OK
            //Content-Type: application/soap+xml; charset=utf-8
            //Content-Length: length

            //<?xml version="1.0" encoding="utf-8"?>
            //<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
            //  <soap12:Body>
            //    <HelloWorld3Response xmlns="http://tempuri.org/">
            //      <HelloWorld3Result>string</HelloWorld3Result>
            //    </HelloWorld3Response>
            //  </soap12:Body>
            //</soap12:Envelope>
            #endregion

            HttpWebRequest httpWebRequest = null;
            string result = null;
            var webserviceurl = "http://localhost:8012/WebService1.asmx" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
            httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
            //注意与SOAP1.1 区分 ContentType,不需要SOAPAction,Content-Type: application/soap+xml; charset=utf-8
            httpWebRequest.ContentType = "application/soap+xml; charset=utf-8";
            httpWebRequest.Method = "post";
            //不需要了 httpWebRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld3");

            Stream requestStream = httpWebRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream);
            streamWriter.Write($"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n<soap12:Body>\r\n<HelloWorld3 xmlns=\"http://tempuri.org/\">\r\n<a>{a}</a>\r\n<b>{b}</b>\r\n</HelloWorld3>\r\n</soap12:Body>\r\n</soap12:Envelope>");
            streamWriter.Close();
            requestStream.Close();

            //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
            //requestStream.Write(vs, 0, vs.Length);
            httpWebRequest.ContentLength = vs.Length;
            //requestStream.Close();

            Stream responseStream = null;
            StreamReader reader = null;
            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            try
            {
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    //返回值类型 Content-Type: application/soap+xml; charset=utf-8
                    //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                    responseStream = webResponse.GetResponseStream();
                    reader = new StreamReader(responseStream);
                    result = reader.ReadToEnd();
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(result);
                    result = xmlDocument.InnerText;
                }
            }
            catch (Exception ex)
            {
                result = $"查询出错,原因:{ex}";
            }
            finally
            {
                reader.Close();
                webResponse.Close();
                responseStream.Close();
                httpWebRequest.Abort();
            }
            return result;
            //if (!string.IsNullOrEmpty(result))
            //{
            //    System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
            //}
            #endregion
        }

        /// <summary>
        /// WebService HTTP方法调用
        /// </summary>
        /// <param name="xmldata">调用方法所需参数</param>        
        public static string WebServiceHTTP(string xmldata)
        {
            //http://localhost:8012/WebService1.asmx/HelloWorld3
            #region HTTP POST 请求和响应示例
            #region HTTP POST 请求和响应示例。所显示的占位符需替换为实际值
            //以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。
            // POST /WebService1.asmx/HelloWorld3 HTTP/1.1
            // Host: localhost
            // Content-Type: application/x-www-form-urlencoded
            // Content-Length: length替换
            // a=string替换&b=string替换

            // HTTP/1.1 200 OK
            // Content-Type: text/xml; charset=utf-8
            // Content-Length: length

            // <?xml version="1.0" encoding="utf-8"?>
            // <string xmlns="http://tempuri.org/">string</string> 
            #endregion

            HttpWebRequest httpWebRequest = null;
            string result = null;
            var webserviceurl = "http://localhost:8012/WebService1.asmx/HelloWorld3" ?? ConfigurationManager.AppSettings.Get("WebServiceUrl");
            httpWebRequest = (HttpWebRequest)WebRequest.Create(webserviceurl);
            //注意与SOAP1.1,SOAP1.2 区分 ContentType,不需要SOAPAction,Content-Type: application/x-www-form-urlencoded
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Method = "post";

            Stream requestStream = httpWebRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream);
            streamWriter.Write(xmldata);
            streamWriter.Close();
            requestStream.Close();

            //byte[] vs = Encoding.UTF8.GetBytes("a=66&b=1233");
            //requestStream.Write(vs, 0, vs.Length);
            httpWebRequest.ContentLength = vs.Length;
            //requestStream.Close();

            Stream responseStream = null;
            StreamReader reader = null;
            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            try
            {
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    //返回值类型 Content-Type: text/xml; charset=utf-8
                    //StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                    responseStream = webResponse.GetResponseStream();
                    reader = new StreamReader(responseStream);
                    result = reader.ReadToEnd();
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(result);
                    result = xmlDocument.InnerText;
                }
            }
            catch (Exception ex)
            {
                result = $"查询出错,原因:{ex}";
            }
            finally
            {
                reader.Close();
                webResponse.Close();
                responseStream.Close();
                httpWebRequest.Abort();
            }
            return result;
            //if (!string.IsNullOrEmpty(result))
            //{
            //    System.Xml.Serialization.XmlSerializer xmlSerializer= new System.Xml.Serialization.XmlSerializer()
            //}
            #endregion
        }
        #endregion

使用代码

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

运行效果

相关推荐
愚润求学9 分钟前
【Linux】动静态库链接原理
linux·运维·服务器·开发语言·笔记
呦呦彬17 分钟前
【问题排查】easyexcel日志打印Empty row!
java·开发语言·log4j
Tummer836327 分钟前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
九章云极AladdinEdu34 分钟前
GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
java·开发语言·人工智能·深度学习·测试工具·负载均衡·transformer
好吃的肘子1 小时前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
ghost1431 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
小白学大数据1 小时前
Scrapy框架下地图爬虫的进度监控与优化策略
开发语言·爬虫·python·scrapy·数据分析
立秋67891 小时前
用Python绘制梦幻星空
开发语言·python·pygame
汉克老师1 小时前
GESP2025年3月认证C++二级( 第三部分编程题(1)等差矩阵)
c++·算法·矩阵·gesp二级·gesp2级
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块
开发语言·青少年编程·rust·编程与数学