当前位置: 首页 > news >正文

使用Python的requests库发送SOAP请求,错误码415

使用Python的requests库发送SOAP请求,错误码415

  • 1. 背景
  • 2. tomcat启动问题
  • 3. 报415错误
  • 4. 按照SOAP格式拼装xml

1. 背景

这个项目之前是采用的Python的requests库以POST的方式向服务器传送json字符串的形式,这次由于合作方的变化,要采用web services的形式,按照SOAP协议,传送XML格式的数据到服务端。

本着少改动的原则,决定继续使用Python的requests库,将原来的json字符串拼成XML格式,再发送给服务端。

在改动的过程中,遇到了一些问题,并逐个进行了解决。下面是详细的过程。

2. tomcat启动问题

  1. 首先有个服务端,合作方已经部署在了tomcat上,并把压缩包发了过来;第一步肯定是先运行tomcat;
  2. 运行tomcat后,localhost:8080显示tomcat已经启动,但是服务的域名localhost:8080/TPService/TPServicePort?wsdl却显示404,如图所示:在这里插入图片描述
  3. 然后发现运行起来的tomcat是9.0版本,而合作方发来的tomcat是7.0版本;
  4. 使用的是windows电脑,将环境变量中的CATALINA_HOME的值改为7.0版本的tomcat文件夹路径,即可启动7.0版本的tomcat,并能够成功访问localhost:8080/TPService/TPServicePort?wsdl在这里插入图片描述

3. 报415错误

  1. 首先并不急于将完整的数据发送给服务端,而是仅仅想把与服务端的连接成功建立。此时的代码大概如下:

    import requests
    url = 'http://localhost:8080/TPService/TPServicePort?wsdl'
    payload = {'a':'test'}
    response = requests.post(url, json=payload, timeout=5)
    
  2. 此时会报415错误;到服务端查看,发现如下报错信息:

    Unsupported Content-Type: application/json Supported ones are: [text/xml]
    com.sun.xml.ws.server.UnsupportedMediaException: Unsupported Content-Type: application/json Supported ones are: [text/xml]
    
  3. 按照提示,增加headers,将content-type的值设置为text/xml,此时代码如下:

    import requests
    from requests.structures import CaseInsensitiveDict
    
    url = 'http://localhost:8080/TPService/TPServicePort?wsdl'
    headers = CaseInsensitiveDict()
    headers['Content-Type'] = 'text/xml'
    payload = {'a': 'b'}
    response = requests.post(url, headers=headers, json=payload, timeout=5)
    print(response.status_code)
    
  4. 此时报500错误,说明通信连接已经建立了;

4. 按照SOAP格式拼装xml

  1. 一开始拼接的xml如下:

    <?xml version ="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
    	<S:Body>
    		<n:uploadTestData xmlns:n="http://localhost:8080/TPService/TPServicePort?wsdl">
    			<a>test</a>
    		</n:uploadTestData>
    	</S:Body>
    </S:Envelope>
    
  2. 此时代码大概如下:

    import requests
    from requests.structures import CaseInsensitiveDict
    
    url = 'http://localhost:8080/TPService/TPServicePort?wsdl'
    headers = CaseInsensitiveDict()
    headers['Content-Type'] = 'text/xml'
    payload = ‘’‘
    <?xml version ="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
    	<S:Body>
    		<n:uploadTestData xmlns:n="http://localhost:8080/TPService/TPServicePort?wsdl">
    			<a>test</a>
    		</n:uploadTestData>
    	</S:Body>
    </S:Envelope>
    ’‘’
    response = requests.post(url, headers=headers, json=payload, timeout=5)
    print(response.status_code)
    
  3. 然后这时会报错500如下:

    Couldn't create SOAP message due to exception: XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,14]
    Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    com.sun.xml.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,14]
    
  4. 按照提示,找到[2, 14]的位置,发现是xml version这里,去掉两个单词之间的空格,再次请求,报错500,但是服务端没有信息显示。

  5. 发现是请求地址写错了,请求地址应与localhost:8080/TPService/TPServicePort?wsdl中的namespace保持一致。将代码修改为:

    import requests
    from requests.structures import CaseInsensitiveDict
    
    url = 'http://localhost:8080/TPService/TPServicePort?wsdl'
    headers = CaseInsensitiveDict()
    headers['Content-Type'] = 'text/xml'
    payload = ‘’‘
    <?xml version ="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
    	<S:Body>
    		<n:uploadTestData xmlns:n="http://www.xxxx.com">
    			<a>test</a>
    		</n:uploadTestData>
    	</S:Body>
    </S:Envelope>
    ’‘’
    response = requests.post(url, headers=headers, json=payload, timeout=5)
    print(response.status_code)
    
  6. 再次发起请求,返回结果如下:

    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    	<S:Body>
    		<ns2:uploadTestDataResponse xmlns:ns2="http://www.glorysoft.com">
    			<return>														{&quot;responseDate&quot;:&quot;20210420114103554&quot;,&quot;resultCode&quot;:&quot;OK&quot;,&quot;resultMessage&quot;:&quot;success&quot;}
    			</return>
         </ns2:uploadTestDataResponse>
       </S:Body>
     </S:Envelope>
    

    说明成功进行了通信。

相关文章:

  • Python爬虫技术系列-02HTML解析-lxml+BS4
  • 今日头条——机器学习算法岗1234面
  • 【笔记】快速理解傅里叶级数
  • 宣布发布 .NET 7 Release Candidate 1
  • 8万Star,这个开源项目有点强
  • 数据批处理速度慢?不妨试试这个
  • 透过安全事件剖析黑客组织攻击技术(2FA/MA的攻击手法)
  • java毕业设计——基于Java+AI的五子棋游戏设计与实现(毕业论文+程序源码)——五子棋游戏
  • 29、Java 中的接口详解
  • mysql中怎么防止数据丢失
  • 软件开发中会使用到的图
  • 汇编语言入门(二)
  • java中的IO流
  • 【每日一练】图解: 数组中的逆序对
  • 【Django】开发日报_8_Day:手机号码管理系统(6)
  • JS 中的深拷贝与浅拷贝
  • python3.6+scrapy+mysql 爬虫实战
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 0x05 Python数据分析,Anaconda八斩刀
  • 2018一半小结一波
  • Android 架构优化~MVP 架构改造
  • CAP 一致性协议及应用解析
  • js如何打印object对象
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • Puppeteer:浏览器控制器
  • 阿里云前端周刊 - 第 26 期
  • 半理解系列--Promise的进化史
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 算法---两个栈实现一个队列
  • 网页视频流m3u8/ts视频下载
  • No resource identifier found for attribute,RxJava之zip操作符
  • elasticsearch-head插件安装
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • ​比特币大跌的 2 个原因
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (3)选择元素——(17)练习(Exercises)
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转)菜鸟学数据库(三)——存储过程
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .Net MVC4 上传大文件,并保存表单
  • .net Stream篇(六)
  • .Net7 环境安装配置
  • @html.ActionLink的几种参数格式
  • @ModelAttribute 注解
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码
  • []C/C++读取串口接收到的数据程序
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [AMQP Connection 127.0.0.1:5672] An unexpected connection driver error occured
  • [BZOJ] 1001: [BeiJing2006]狼抓兔子
  • [C#]winform使用引导APSF和梯度自适应卷积增强夜间雾图像的可见性算法实现夜间雾霾图像的可见度增强