首页 > 其他分享 >WebService SOAP1.1 SOAP1.12 HTTP PSOT方式调用

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

时间:2023-10-23 21:12:34浏览次数:38  
标签:PSOT HTTP WebService Content result httpWebRequest Close string

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

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

运行效果:

 

标签:PSOT,HTTP,WebService,Content,result,httpWebRequest,Close,string
From: https://www.cnblogs.com/1175429393wljblog/p/17783486.html

相关文章

  • mac 以https的方式启动打包的静态资源
    1npxserve2 brewinstallmkcert 3新建目录,存放证书,在新目录执行,mkcert192.168.31.66 (本机ip)4pwd查看当前目录5在项目执行 npxserve--ssl-cert/Users/da/Desktop/dyl/doc/ssl/192.168.31.66.pem--ssl-key/Users/da/Desktop/dyl/doc/ssl/192.168.31.66-key.pem......
  • laravel:单元测试之http测试(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/http-tests/14896二,php代码:1,创建test程序liuhongdi@lhdpc:/data/laravel/dignews$phpartisanmake:testNewsTest   INFO  Test[tests/Feature/NewsTest.php]createdsuccessfully.2,代码:12345......
  • 老大加需求:做一个支持超大文件 HTTP 断点续传的上传服务,我懵逼了~
    作者:大飞飞鱼来源:blog.csdn.net/ababab12345/article/details/80490621Part1前言最近由于笔者所在的研发集团产品需要,需要支持高性能的大文件(大都数是4GB以上)的http上传,并且要求支持http断点续传。笔者在以前的博客如何实现支持大文件的高性能HTTP文件上传服务器已经介绍了......
  • 前端接口请求HTTP设置自定义header属性字段大小写问题
    问题:接口请求头传token值的字段为tokenValue,需要用到token的接口一直不能成功请求。后端排查发现没有接收到token,前端虽然传了token值,但是发现浏览器把tokenValue变成了Tokenvalue,导致后端没正确接收到token值。原因是:HTTPRFC里规定,大小写不敏感。HTTP/1.x大小写不敏感,但现实是......
  • Linux编译安装 drogon(高性能http服务器)
    实际上还是建议用Ubuntu进行编译,要方便的多drogon编译安装:https://zhuanlan.zhihu.com/p/601632372drogon编译安装:https://wenku.baidu.com/view/4408ed4e84c24028915f804d2b160b4e777f8150.html一、编译前置依赖项目jsoncpp#项目地址:https://github.com/open-source-pars......
  • Https 安全协议版本支持检测工具
    需求:帮朋友做了一个小工具,主要目的是检查局域网(在线检查网站无法访问内网)的https站点配置的安全协议版本,方便给第三方提供接口文档,描述https站点所使用的安全协议。相信大家在开发过程中,可能会遇到“Therequestwasaborted:CouldnotcreateSSL/TLSsecurechannel”异常,......
  • HTTPS 的加密流程
    HTTPS是在HTTP的基础上进行了一层加密,加密就是把明文(要传输的信息)进行一系列变换,生成密文。解密就是把密文再进行一系列变换,还原成明文。在这个加密和解密的过程中,往往需要一个或者多个中间的数据,辅助进行这个过程,这样的数据称为密钥。HTTPS的工作过程既然要......
  • web入门-HTTP协议
    web入门-HTTP协议-概述HTTP概念:HyperTextTransferProtocol,超文本传输协议,规定了浏览器和服务器之间数据传输的规则。特点:基于TCP协议:面向连接,安全基于请求-响应模型的:一次请求对应一次响应HTTP协议是无状态的协议,对于事务处理没有记忆能力。每次请求-响应都是独立的。......
  • Uni-App Http请求
    该插件适用于一般的请求场景,只支持post、get、put和delete请求,目前不适用于其他的请求形式,比如上传,下载等。插件定位为小而美,而不是大而全,目标是切合实际,开箱即用。平台差异说明AppH5微信小程序支付宝小程序百度小程序头条小程序QQ小程序√√√√√√√由于某......
  • NGINX 安装 SSL 证书 - 开启 HTTPS 访问
    在NGINX上安装SSL证书-开启HTTPS访问一、在你开始之前请确保您已下载证书文件。没有证书?请从https://www.sslforfree.com上申请90天免费证书。下载证书后,您应该有一个包含以下证书文件的ZIP:certificate.crtca_bundle.crtprivate.key二、上传证书文件首先,......