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

news2024/10/4 4:20:34

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

  #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

使用代码

                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}");

运行效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1125071.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Markdown语法详解

文章目录 [toc] 一、简介二、样式1. 标题2. 字体3. 引用4. 分割线5. 图片6. 超链接7. 列表8. 表格9. 代码 一、简介 以前写学习文档常用的软件都是Word或者CSDN自带的编辑器&#xff0c;但Word用起来不太灵活&#xff0c;而CSDN自带编辑器又感觉逼格不够&#xff08;主要原因&…

(自我剖析一下我博客“问答”中的第三个问题)准确率一直居低不上是什么原因引起的?

我提的问题是&#xff1a; “我使用单层GRU训练minist数据集时&#xff0c;准确率一直处于下图的状态是为什么&#xff1f; 什么原因引起的&#xff1f;” 这种debug就比较难受&#xff0c;因为程序是能跑的&#xff0c;任何“error”都没有出。这就表明在程序中有某些小细节没…

【SwiftUI模块】0060、SwiftUI基于Firebase搭建一个类似InstagramApp 3/7部分-搭建TabBar

SwiftUI模块系列 - 已更新60篇 SwiftUI项目 - 已更新5个项目 往期Demo源码下载 技术:SwiftUI、SwiftUI4.0、Instagram、Firebase 运行环境: SwiftUI4.0 Xcode14 MacOS12.6 iPhone Simulator iPhone 14 Pro Max SwiftUI基于Firebase搭建一个类似InstagramApp 3/7部分-搭建Tab…

数据集的特征提取

1、 特征提取 1.1、 将任意数据&#xff08;如文本或图像&#xff09;转换为可用于机器学习的数字特征 注&#xff1a;特征值化是为了计算机更好的去理解数据 字典特征提取(特征离散化)文本特征提取图像特征提取&#xff08;深度学习将介绍&#xff09; 2 特征提取API sklear…

Python OpenCV通过灰度平均值进行二值化处理以减少像素误差

Python OpenCV通过灰度平均值进行二值化处理以减少像素误差 前言前提条件相关介绍实验环境通过灰度平均值进行二值化处理以减少像素误差固定阈值二值化代码实现 灰度平均值二值化代码实现 前言 由于本人水平有限&#xff0c;难免出现错漏&#xff0c;敬请批评改正。更多精彩内容…

数据安全与PostgreSQL:最佳保护策略

在当今数字化时代&#xff0c;数据安全成为了企业不可或缺的一环。特别是对于使用数据库管理系统&#xff08;DBMS&#xff09;的组织来说&#xff0c;确保数据的完整性、保密性和可用性至关重要。在众多DBMS中&#xff0c;PostgreSQL作为一个强大而灵活的开源数据库系统&#…

酒类商城小程序怎么做

随着互联网的快速发展&#xff0c;线上购物越来越普及。酒类商品也慢慢转向线上销售&#xff0c;如何搭建一个属于自己的酒类小程序商城呢&#xff1f;下面就让我们一起来看看吧&#xff01; 一、登录乔拓云平台 首先&#xff0c;我们需要进入乔拓云平台的后台&#xff0c;点击…

Pytorch公共数据集、tensorboard、DataLoader使用

本文将主要介绍torchvision.datasets的使用&#xff0c;并以CIFAR-10为例进行介绍&#xff0c;对可视化工具tensorboard进行介绍&#xff0c;包括安装&#xff0c;使用&#xff0c;可视化过程等&#xff0c;最后介绍DataLoader的使用。希望对你有帮助 Pytorch公共数据集 torc…

【第三天】C++类和对象进阶指南:从堆区空间操作到友元的深度掌握

一、new和delete 堆区空间操作 1、new和delete操作基本类型的空间 new与C语言中malloc、delete和C语言中free 作用基本相同 区别&#xff1a; new 不用强制类型转换 new在申请空间的时候可以 初始化空间内容 2、 new申请基本类型的数组 3、new和delete操作类的空间 4、new申请…

【SwiftUI模块】0060、SwiftUI基于Firebase搭建一个类似InstagramApp 2/7部分-搭建TabBar

SwiftUI模块系列 - 已更新60篇 SwiftUI项目 - 已更新5个项目 往期Demo源码下载 技术:SwiftUI、SwiftUI4.0、Instagram、Firebase 运行环境: SwiftUI4.0 Xcode14 MacOS12.6 iPhone Simulator iPhone 14 Pro Max SwiftUI基于Firebase搭建一个类似InstagramApp 2/7部分-搭建Tab…

构建离线应用:Apollo与本地状态管理

前言 「作者主页」&#xff1a;雪碧有白泡泡 「个人网站」&#xff1a;雪碧的个人网站 「推荐专栏」&#xff1a; ★java一站式服务 ★ ★ React从入门到精通★ ★前端炫酷代码分享 ★ ★ 从0到英雄&#xff0c;vue成神之路★ ★ uniapp-从构建到提升★ ★ 从0到英雄&#xff…

网络流探索:解决网络最大流问题的算法集锦

1.初识网络流 网络流一直是初学者心中很难过去的一道坎&#xff0c;很多人说它是一个不像DFS/BFS那么直观的算法&#xff0c;同时网上也有各种参差不齐的材料&#xff0c;让人感到一知半解。 如果你也有这样的感觉&#xff0c;那么不要灰心&#xff0c;坚持住&#xff0c;因为…

【仙逆】王林用计灭富二代,有长命锁也没用,藤化元一怒请一人出山

【侵权联系删除】【文/郑尔巴金】 仙逆动漫第七集已经更新了。而这一集看下来&#xff0c;可以说非常精彩&#xff0c;全程在打&#xff0c;期间还能看到主角王林用谋&#xff0c;是如何一步步的把敌人藤厉引入陷阱灭杀的&#xff0c;更可以看到王林是如何筑基的。那么多的不说…

Windows系统下安装CouchDB3.3.2教程

安装 前往CouchDB官网 官网点击download下载msi文件 双击该msi文件&#xff0c;一直下一步 创建个人account 设置cookie value 用于进行身份验证和授权。 愉快下载 点击OK 重启 启动 重启电脑后 打开浏览器并访问以下链接&#xff1a;http://127.0.0.1:5984/ 如果没有问…

SQL UPDATE 语句(更新表中的记录)

SQL UPDATE 语句 UPDATE 语句用于更新表中已存在的记录。 还可以使用AND或OR运算符组合多个条件。 SQL UPDATE 语法 具有WHERE子句的UPDATE查询的基本语法如下所示&#xff1a; UPDATE table_name SET column1 value1, column2 value2, ... WHERE conditi…

编写第一个python程序

实践流程 新建程序 ↓选择“file”\"new feile"命令 向世界问好 ↓print&#xff08;‘hello word’&#xff09; 保存&#xff0c;运行程序 ↓按“F5键”运行程序 修改程序 ↓print(hello word\n *20) 常见错误 中文错误 熟悉python idle编程环境 学习流程…

Go学习第五章——函数与包

Go学习第五章——函数与包 1 函数1.1 基本语法1.2 函数多返回值1.3 函数的可见性和包级函数1.4 函数调用机制底层原理1.5 值类型和引用类型1.6 注意事项和细节1.7 逃逸机制&#xff08;补&#xff0c;可不看&#xff09; 2 包2.1 快速入门2.2 包的使用细节 3 函数详细讲解3.1 递…

3BHE009319R0001 UNS2881b-P,V1 l展示面向边缘人工智能应用

3BHE009319R0001 UNS2881b-P,V1 l展示面向边缘人工智能应用 ASRock Industrial展示了由第12代英特尔酷睿处理器(Alder Lake-P)支持的NUC 1200 BOX/iBOX 1200系列工业计算机&#xff0c;用于跨行业的人工智能加速。工业计算机采用高达12个内核/16个线程的高性能混合架构和高达9…

MySQL -- 库和表的操作

MySQL – 库和表的操作 文章目录 MySQL -- 库和表的操作一、库的操作1.创建数据库2.查看数据库3.删除数据库4.字符集和校验规则5.校验规则对数据库的影响6.修改数据库7.备份和恢复8.查看连接情况 二、表的操作1.创建表2.查看表结构3.修改表4.删除表 一、库的操作 注意&#xf…

针对遗留系统采取的不同演化策略

1. 改造策略 高水平、高价值区&#xff0c;即遗留系统的技术含量较高&#xff0c;本身还有极大的生命力。系统具有较高的业务价值&#xff0c;基本上能够满足企业业务运作和决策支持的需要。这种系统可能建成的时间还很短&#xff0c;对这种遗留系统的演化策略为改造。改造包括…