.NetCore调用带密码验证的Webservice_Walk in loosing的博客-程序员宅基地

技术标签: linux  DotNetCore  webservice  

DotNetCore用工具生成的代理类按下图设置用户密码访问webservice一直报用户密码验证不通过。
在这里插入图片描述
查了几天资料都无法突破。为此回到webservice的soap协议的本身来解决。soap的本质就是发布http服务接收按soap文档约定的xml串,然后按xml执行指定方法后返回xml个http调用客户端。然后就用httpclient来调用webservice就行了。

首先下载SOAPUI工具可以查看webservice请求的xml格式,还能测试调用,写代码前可以先用xml测试调用通了再写实现代码。
在这里插入图片描述
带用户密码验证的请求示例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <soap:Header>
        <wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetData</wsa:Action>
        <wsa:ReplyTo>
            <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
        </wsa:ReplyTo>
        <wsa:To>http://127.0.0.1:57772/imedicallis/csp/LIS.WS.DHCLISService.cls</wsa:To>
        <wsse:Security soap:mustUnderstand="1">
            <wsse:UsernameToken
                xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec">
                <wsse:Username>_SYSTEM1</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SYS</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <GetData xmlns ="http://tempuri.org">
            <ClassName>Service.LIS.QC.DHCQCService</ClassName>
            <FuncName >GetMachineList</FuncName>
            <Param >
               
            </Param>
            <Session ></Session>
        </GetData >
    </soap:Body>
</soap:Envelope>

也能把调用服务的请求端口和TCPTrace工具配置的监听端口一致然后用这个工具抓包看别的程序成功请求的提交XML串。基于这个串再到SOAPUI测试直到测通。
在这里插入图片描述
然后就实现DotNetCore的调用代理类即可

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Xml;

namespace LIS.DAL.DataAccess
{
    
    ///<summary  NoteObject="Class">
    /// [功能描述: 按soap协议自定义调用webservice客户端]<br></br>
    /// [创建者:   张联珠]<br></br>
    /// [创建时间: 2021-3-12]<br></br>
    /// <说明>
    ///    
    /// </说明>
    /// <修改记录>
    ///     <修改时间></修改时间>
    ///     <修改内容>
    ///            
    ///     </修改内容>
    /// </修改记录>
    /// </summary>
    public class MyWebserviceClient
    {
    
        /// <summary>
        /// 服务地址
        /// </summary>
        private string Address = "";

        /// <summary>
        /// 用户名
        /// </summary>
        private string UserName = "";

        /// <summary>
        /// 用户密码
        /// </summary>
        private string UserPass = "";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="address"></param>
        /// <param name="userName"></param>
        /// <param name="userPass"></param>
        public MyWebserviceClient(string address, string userName, string userPass)
        {
    
            Address = address;
            UserName = userName;
            UserPass = userPass;
        }

        /// <summary>
        /// 查询M得到数据
        /// </summary>
        /// <param name="ClassName">类名</param>
        /// <param name="FuncName">方法名</param>
        /// <param name="Param">参数</param>
        /// <param name="Session">会话</param>
        /// <returns></returns>
        public string GetDataAsync(string ClassName, string FuncName, string Param, string Session)
        {
    
            string result = string.Empty;
            try
            {
    
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                xml += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">";
                xml += "<soap:Header>";
                xml += "<wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetData</wsa:Action>";
                xml += "<wsa:ReplyTo>";
                xml += "<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>";
                xml += "</wsa:ReplyTo> ";
                xml += "<wsa:To>" + Address + "</wsa:To>";
                xml += "<wsse:Security soap:mustUnderstand=\"1\">";
                xml += "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec\">";
                xml += "<wsse:Username>" + UserName + "</wsse:Username>";
                xml += "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" + UserPass + "</wsse:Password>";
                xml += "</wsse:UsernameToken>";
                xml += "</wsse:Security>";
                xml += "</soap:Header>";
                xml += "<soap:Body>";
                xml += "<GetData xmlns =\"http://tempuri.org\">";
                xml += "<ClassName>" + ClassName + "</ClassName>";
                xml += "<FuncName >" + FuncName + "</FuncName>";
                xml += "<Param ><![CDATA[" + Param + "]]></Param>";
                xml += "<Session >" + Session + "</Session>";
                xml += "</GetData >";
                xml += "</soap:Body>";
                xml += "</soap:Envelope>";
                HttpContent content = new StringContent(xml, Encoding.UTF8, "text/xml");
                content.Headers.Add("SOAPAction", "http://tempuri.org/LIS.WS.DHCLISService.GetData");
                using (HttpClient client = new HttpClient())

                using (var response = client.PostAsync(Address, content))
                {
    
                    result = response.Result.Content.ReadAsStringAsync().Result;
                    //创建一个xml文档
                    XmlDocument xmlDoc = new XmlDocument();
                    //为文档导入数据
                    xmlDoc.LoadXml(result);
                    result = xmlDoc.InnerText;
                    //调用报错了
                    if (!result.Contains("<Response>"))
                    {
    
                        result = "<Response><SQLResult><SQL><FunRet></FunRet></SQL></SQLResult><RetVal>-1</RetVal><Error>" + result + "</Error><Node></Node><RowCount>0</RowCount></Response>";
                    }
                }
            }
            catch (Exception ex)
            {
    
                result = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 查询M得到SQL执行结果
        /// </summary>
        /// <param name="SQLText">SQL串</param>
        /// <param name="Param">参数</param>
        /// <param name="Session">会话</param>
        /// <returns>返回串</returns>
        public string GetSQLDataAsync(string SQLText, string Param, string Session)
        {
    
            string result = string.Empty;
            try
            {
    
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                xml += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">";
                xml += "<soap:Header>";
                xml += "<wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetSQLData</wsa:Action>";
                xml += "<wsa:ReplyTo>";
                xml += "<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>";
                xml += "</wsa:ReplyTo> ";
                xml += "<wsa:To>" + Address + "</wsa:To>";
                xml += "<wsse:Security soap:mustUnderstand=\"1\">";
                xml += "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec\">";
                xml += "<wsse:Username>" + UserName + "</wsse:Username>";
                xml += "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" + UserPass + "</wsse:Password>";
                xml += "</wsse:UsernameToken>";
                xml += "</wsse:Security>";
                xml += "</soap:Header>";
                xml += "<soap:Body>";
                xml += "<GetSQLData xmlns = \"http://tempuri.org\">";
                xml += "<SQLText>" + SQLText + "</SQLText>";
                xml += "<Param>" + Param + "</Param >";
                xml += "<Session>" + Session + "</Session>";
                xml += "</GetSQLData>";
                xml += "</soap:Body>";
                xml += "</soap:Envelope>";
                HttpContent content = new StringContent(xml, Encoding.UTF8, "text/xml");
                content.Headers.Add("SOAPAction", "http://tempuri.org/LIS.WS.DHCLISService.GetSQLData");
                using (HttpClient client = new HttpClient())

                using (var response = client.PostAsync(Address, content))
                {
    
                    result = response.Result.Content.ReadAsStringAsync().Result;
                    //创建一个xml文档
                    XmlDocument xmlDoc = new XmlDocument();
                    //为文档导入数据
                    xmlDoc.LoadXml(result);
                    result = xmlDoc.InnerText;
                    //调用报错了
                    if(!result.Contains("<Response>"))
                    {
    
                        result = "<Response><SQLResult><SQL><FunRet></FunRet></SQL></SQLResult><RetVal>-1</RetVal><Error>"+result+"</Error><Node></Node><RowCount>0</RowCount></Response>";
                    }
                }
            }
            catch (Exception ex)
            {
    
                result = ex.Message;
            }

            return result;
        }
    }
}

这样就可以调通了额,适应所有webservice调用情况,要点就是回归soap协议,用httpclient发soapxml。结合抓包工具和soapui测试。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhanglianzhu_91/article/details/114696818

智能推荐

数据交换格式学习笔记-程序员宅基地

数据交换格式主要是指计算机各个程序之间相互交换数据要形成的一种规范,比如xml和Json常用来作为应用程序之间数据交换的方式,一般只有很少的数据量时,可以采用纯文本的方式交换数据,但是数据量大的时候,会导致交换信息的紊乱,所以就产生了将信息分类然后在传输,这样解析数据更加方便,现在主要学习xml文档以及json文档,xml文档:基本架构:1、声明,例如:,它表示使用的是UTF-8字

(CVPR 2017) Joint 3D Proposal Generation and Object Detection from View Aggregation_fish小余儿的博客-程序员宅基地

摘要我们提出了AVOD,一种用于自动驾驶场景的聚合视图目标检测网络。所提出的神经网络架构使用LIDAR点云和RGB图像来生成由两个子网络共享的特征:区域proposal网络(RPN)和第二阶段检测器网络。所提出的RPN使用一种新颖的架构,能够在高分辨率特征图上执行多模态特征融合,从而为道路场景中的多个目标类别生成可靠的3D目标proposal。使用这些proposal,第二阶段检测网络执行准确的定向3D边界框回归和类别分类,以预测3D空间中目标的范围、方向和分类。我们提出的架构被证明可以在KITTI 3D_joint 3d proposal generation and object detection from view aggregation

阿里工作十年的Android架构师,总结出2020最新阿里、腾讯、字节、京东等一线大厂高频面试真题合集,以及从业心得分享_android 架构师面试题-程序员宅基地

前言:本文资料是由阿里工作十年架构师提供,属于纯干货篇,总结了最新2020整理收集的一些面试题(都整理成文档,附答案),涵盖了阿里巴巴、腾讯、字节跳动、京东、华为等大厂的Android面试真题,和资深架构师学习路线以及Android架构文档。不管你是要面试大厂还是普通的互联网公司,这些面试题对你肯定是有帮助的,毕竟大厂一定是行业的发展方向标杆,很多公司的面试官同样会研究大厂的面试题。也会对你的架构师之路带来帮助,文中更是有详细的学习规划图,给你带来不一样的阅读感。干货献上:第一部分:(Android_android 架构师面试题

Python Web框架Tornado运行和部署-程序员宅基地

为什么80%的码农都做不了架构师?>>> ..._稳定部署tornado

WebService基于SoapHeader实现安全认证(一)-程序员宅基地

本文转载:http://www.cnblogs.com/houleixx/archive/2009/08/22/webservice-soapheader-security.htmlWebService基于SoapHeader实现安全认证 本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来...

Docker中关于-Djava.security.egd=file:/dev/./urandom参数的疑问_docker file:/dev/./urandom-程序员宅基地

在学习DockerFile中看到[ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]中的-Djava.security.egd=file:/dev/./urandom就产生了疑问,日常启动springboot项目没有这个参数。然后对这个参数进行的搜索,还是有文章说明到了这个参数用于产生随机数,又不禁的疑问为什么要产生随机数。还是在stackoverflow.com的网站中看到了英文的解释。如下:T_docker file:/dev/./urandom

随便推点

EDA数字钟设计(verilog)——显示译码模块_eda显示译码-程序员宅基地

进行正常的示数和闹钟的示数,利用电路的共阳极数码管的特性进行10个数字和“-”的译码,分别利用两位16进制进行定义,更加简约方便,闹钟电路和正常示数分开显示,防止优先级之间的干扰,动态显示电路的频率为1KHz,超过了人眼可以分辨出来的频率。利用除法和求余进行个位和十位的提取,使代码变得简单易懂。具体代码如下:module xianshi(clk_1KHz,second,min..._eda显示译码

d3d学习之四 hdr_d3d hdr-程序员宅基地

下面这个图片应该是比较好的反应了hdr的处理过程的流程大约如下:1.首先获得high range的一个floating-point hdr texture 这个过程就是应用普通的光照模型,对于输出的为一个支持浮点数的纹理,不会对RGBA值截断,逼近现实世界的颜色2.对1得到的texture缩小到1/4 (这个大约是为了节约处理时间,是否可以变得更小呢)的scaled copy,_d3d hdr

RecyclerView-程序员宅基地

MainActivity代码//找到控件idrv = findViewById(R.id.rv);myadapter = new Myadapter(MainActivity.this, list);//给控件设置适配器rv.setAdapter(myadapter);// 类名.方法名调用 参数为接口的方法 new 类名.接口的名字 此为点击事件的方法

Hibernate java.lang.NoSuchFieldError: INSTANCE 解决方法-程序员宅基地

Hibernate java.lang.NoSuchFieldError: INSTANCE 在使用hibernate3.6.2是我遇到了一个有趣的错误java.lang.NoSuchFieldError: INSTANCEat org.hibernate.type.BasicTypeRegistry.&lt;init&gt;(BasicTypeRegistry.java:94)at org....

windows下opencv4.2.0编译安装_win 11 opencv 4.2.0.0-程序员宅基地

1.下载Cmakehttps://cmake.org/download/2.下载opencv4.2.0https://opencv.org/releases/3.下载opencv_contrib模块https://github.com/opencv/opencv_contrib4.编译设定:重点:设置地址不要复制,要用图形界面选择路径5.alarm的结解除-download..._win 11 opencv 4.2.0.0

二叉树中序遍历递归与非递归-程序员宅基地

先序遍历的访问顺序为 根节点-&amp;amp;amp;gt;左子树-&amp;amp;amp;gt;右子数递归的方式很简单,这里就不叙述了,考虑非递归方式思路:中序遍历,先访问左子树,访问左子树的时候,其父节点得存在堆栈里,等到左子树访问完毕后再讲堆栈中的父节点弹出,对其进行访问,访问完然后在访问其右子树核心代码 res = [] pNode = self.root stack = [] ...

推荐文章

热门文章

相关标签