C#项目WCF接口暴露调用及SOAP接口请求测试(Python版)

C#项目WCF接口暴露调用

(1)定义WCF服务(IClear.cs),在ServiceContract定义服务

(2)实现WCF服务,在服务的实现类中定义具体的实现

(3)配置WCF服务,在App.config中配置服务端点

复制代码
<system.serviceModel>
    <bindings>
        <!-- netTcpBinding 配置 -->
        <netTcpBinding>
            <binding name="BindingBehaviorConfiguration"
                     portSharingEnabled="false"
                     maxBufferSize="2147483647"
                     maxReceivedMessageSize="2147483647"
                     openTimeout="00:10:00"
                     receiveTimeout="00:10:00"
                     sendTimeout="00:10:00"
                     closeTimeout="00:10:00">
                <security mode="None" />
            </binding>
        </netTcpBinding>

        <!-- basicHttpBinding 配置,用于HTTP SOAP -->
        <basicHttpBinding>
            <binding name="WESSSoap" />
        </basicHttpBinding>
    </bindings>

    <services>
        <service name="WZ.WCPD.DataClear.Clear.Communication.ClearService">
            <!-- TCP端点,使用 netTcpBinding -->
            <endpoint address="net.tcp://localhost:3088/ClearService"
                      binding="netTcpBinding"
                      contract="WZ.WCPD.DataCommon.Interface.IClear"
                      bindingConfiguration="BindingBehaviorConfiguration" />

            <!-- HTTP端点,使用 basicHttpBinding,适用于Python请求 -->
            <endpoint address="http://localhost:3006/ClearService"
                      binding="basicHttpBinding"
                      contract="WZ.WCPD.DataCommon.Interface.IClear"
                      bindingConfiguration="WESSSoap" />
        </service>
    </services>
</system.serviceModel>

注意:

netTcpBinding 配置

netTcpBinding是一种TCP协议的绑定,它用于通过TCP协议进行通信,而不是通过HTTP/HTTPS(SOAP通常是基于HTTP的协议)。

该配置没有涉及SOAPAction,因为netTcpBinding不使用SOAP,它是基于二进制协议的。

basicHttpBinding 配置

  • basicHttpBinding是一个基于HTTP的绑定,用于SOAP协议的通信。它通常会使用SOAP消息格式,但在此配置中没有显式的SOAPAction或其他SOAP特定的设置。

(4)运行WCF服务

(5)使用Python进行SOAP接口测试

python使用SOAP请求WCF接口

SOAP简介

SOAP(Simple Object Accrss Protocol,简单对象访问协议)是一种简单的基于XML的协议,可以使应用程序在分散或分布式的环境中通过HTTP来交换信息。或者简单的说: SOAP 是基于 XML 的 Web Services 间的通信协议。

SOAP基于XML语言和XSD标准,其定义了一套编码规则,编码规则定义如何将数据表示为消息,以及怎样通过HTTP协议来传输SOAP消息,由四部分组成:

(1) SOAP信封(Envelope):定义了一个框架,框架描述了消息中的内容是什么,包括消息的内容、发送者、接收者、处理者以及如何处理消息。

(2)SOAP编码规则:定义了一种系列化机制,用于交换应用程序所定义的数据类型的实例。

(3) SOAP RPC表示:定义了用于表示远程过程调用和应答协定。

(4)SOAP绑定:定义了一种使用底层传输协议来完成在节点间交换SOAP信封的约定。

SOAP消息基本上是从发送端到接收端的单向传输,常常结合起来执行类似于请求/应答的模式。不需要把SOAP消息绑定到特定的协议,SOAP可以运行在任何其他传输协议(HTTP、SMTP、FTP等)上。另外,SOAP提供了标准的RPC方法来调用Web Service以请求/响应模式运行。

SOAP是Web Service的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,可以支持不同的底层接口,像HTTP(S)或者SMTP。

应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信的方式会产生兼容性以及安全问题;防火墙和代理服务器通常会阻止此类流量。通过 HTTP 在应用程序间通信是更好的方法,因为 HTTP 得到了所有的因特网浏览器及服务器的支持。SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

python使用soap测试的几种方式:

(1)无参数请求方式

C#接口信息

使用SOAP请求

python请求代码

复制代码
def getAllEquipmentRunStates():
    # WCF服务的URL,指向HTTP端点
    url = "http://localhos:3006/ClearService"  # 使用配置的HTTP端口

    # SOAP请求体
    soap_request = """<?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:web="http://tempuri.org/">
       <soapenv:Header/>
       <soapenv:Body>
          <web:GetAllEquipmentRunStates/>  <!-- 调用的方法 -->
       </soapenv:Body>
    </soapenv:Envelope>"""

    # 请求头
    headers = {
        "Content-Type": "text/xml;charset=UTF-8",  # 设置SOAP请求内容类型
        "SOAPAction": "http://tempuri.org/IClear/GetAllEquipmentRunStates"  # 根据WCF服务的SOAPAction配置
    }

    # 发送SOAP请求
    response = requests.post(url, data=soap_request, headers=headers)

    # 打印响应内容
    print("Response Status Code:", response.status_code)
    print("Response Body:", response.text)

此时要注意soap_request请求体里面的数据信息,由于C#里面WCF接口没有参数,请求体直接使用<web:GetAllEquipmentRunStates/>,SOAPAction的数据为http://tempuri.org/加上IClear/GetAllEquipmentRunStates

(2)有参数形式,普通参数

复制代码
def getEquipmentRunState():
    # 动态参数  20230824170549465267577960771cd
    objID = "202308241705023453195d77f9e1646"
    # SOAP请求体
    soap_request = f"""<?xml version="1.0" encoding="utf-8"?>
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                          xmlns:web="http://tempuri.org/">
           <soapenv:Header/>
           <soapenv:Body>
              <web:GetEquipmentRunState>
                 <web:objID>{objID}</web:objID>
              </web:GetEquipmentRunState>
           </soapenv:Body>
        </soapenv:Envelope>"""
    # 请求头
    headers = {
        "Content-Type": "text/xml;charset=UTF-8",  # 设置SOAP请求内容类型
        "SOAPAction": "http://tempuri.org/IClear/GetEquipmentRunState"  # 根据WCF服务的SOAPAction配置
    }
    # 发送SOAP请求
    response = requests.post(WCFUrl, data=soap_request, headers=headers)
    # 打印响应内容
    print("Response Status Code:", response.status_code)
    print("Response Body:", response.text)

(3)list参数请求

复制代码
def getEquipmentRunStates():
    # 动态参数
    objIDs = ["20230824170549465267577960771cd", "202308241705023453195d77f9e1646"]
    # 构建 objIDs 部分的 XML
    obj_ids_xml = "\n".join([f"<wcf:string>{id}</wcf:string>" for id in objIDs])
    # SOAP请求体    {"".join([f"<web:objID>{objID}</web:objID>" for objID in objIDs])}
    # xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    soap_request = f"""<?xml version="1.0" encoding="utf-8"?>
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                          xmlns:web="http://tempuri.org/"
                          xmlns:wcf="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
           <soapenv:Header/>
           <soapenv:Body>
              <web:GetEquipmentRunStates>
                <web:objIDs>
                    {"".join([f"<wcf:string>{id}</wcf:string>" for id in objIDs])}
                </web:objIDs>
              </web:GetEquipmentRunStates>
           </soapenv:Body>
        </soapenv:Envelope>"""
    # 请求头
    headers = {
        "Content-Type": "text/xml;charset=UTF-8",  # 设置SOAP请求内容类型
        "SOAPAction": "http://tempuri.org/IClear/GetEquipmentRunStates"  # 根据WCF服务的SOAPAction配置
    }
    # 发送SOAP请求
    response = requests.post(WCFUrl, data=soap_request, headers=headers)
    # 打印响应内容
    print("Response Status Code:", response.status_code)
    print("Response Body:", response.text)

注意由于objIDs为List<string>格式,所以请求方式特殊,具体方式有如下:

( 1)动态变化参数(推荐使用)

( 2)外部动态变化参数

( 3)内部固定参数

4)自定义实体类

实体类EquVar

方式一:固定方式

复制代码
def getHistoryDataNew():
    # SOAP请求体
    soap_request = f"""<?xml version="1.0" encoding="utf-8"?>
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                              xmlns:web="http://tempuri.org/"
                              xmlns:wcf="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
                              xmlns:dat="http://schemas.datacontract.org/2004/07/WZ.WCPD.DataCommon.Model">
               <soapenv:Header/>
               <soapenv:Body>
                  <web:GetHistoryDataNew>
                    <web:equVarList>
                        <dat:EquVar>
                            <dat:EquID>20230824170549465267577960771cd</dat:EquID>
                            <dat:VarID>30</dat:VarID>
                            <dat:CurTime>2025-08-14T10:00:00</dat:CurTime>
                        </dat:EquVar>
                        <dat:EquVar>
                            <dat:EquID>202308241705023453195d77f9e1646</dat:EquID>
                            <dat:VarID>30</dat:VarID>
                            <dat:CurTime>2025-08-14T10:00:00</dat:CurTime>
                        </dat:EquVar>
                    </web:equVarList>
                  </web:GetHistoryDataNew>
               </soapenv:Body>
            </soapenv:Envelope>"""
    # 请求头
    headers = {
        "Content-Type": "text/xml;charset=UTF-8",  # 设置SOAP请求内容类型
        "SOAPAction": "http://tempuri.org/IClear/GetHistoryDataNew"  # 根据WCF服务的SOAPAction配置
    }
    # 发送SOAP请求
    response = requests.post(WCFUrl, data=soap_request, headers=headers)
    # 打印响应内容
    # print("Response Status Code:", response.status_code)
    # print("Response Body:", response.text)

注意:自定义的实体类EquVar要和配置里面的实体类对象,尤其是dat要对应

方式二:动态实体类

注意xmlns:dat="http://schemas.datacontract.org/2004/07/WZ.WCPD.DataCommon.Model" 要和C#里面的实体类所在的位置对应。

相关推荐
张登杰踩1 天前
VIA标注格式转Labelme标注格式
python
Learner1 天前
Python数据类型(四):字典
python
odoo中国1 天前
Odoo 19 模块结构概述
开发语言·python·module·odoo·核心组件·py文件按
Jelena157795857921 天前
Java爬虫api接口测试
python
necessary6531 天前
使用Clion查看linux环境中的PG源码
linux·运维·服务器
代码N年归来仍是新手村成员1 天前
【Java转Go】即时通信系统代码分析(一)基础Server 构建
java·开发语言·golang
踩坑记录1 天前
leetcode hot100 3.无重复字符的最长子串 medium 滑动窗口(双指针)
python·leetcode
Z1Jxxx1 天前
01序列01序列
开发语言·c++·算法
沐知全栈开发1 天前
C语言中的强制类型转换
开发语言
关于不上作者榜就原神启动那件事1 天前
Java中大量数据Excel导入导出的实现方案
java·开发语言·excel