java使用POST发送soap报文请求webservice返回500错误解析
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据,
对错误Server returned HTTP response code: 500 的解决方法进行简单分析。
问题描述:
由于课程需要博主需要自己写一个webservice并且通过soap进行请求,
于是使用JAX-WS编译了下面java代码生成webservice服务
生成webservice的java代码:
- @WebService()
- public class HelloWorld {
- @WebMethod
- public String sayHelloWorldFrom(String from) {
- System.out.println("getMessage.");
- String result = "Hello, world, from " + from;
- System.out.println(result);
- return result;
- }
- public static void main(String[] argv) {
- System.out.println("Service is running...");
- Object implementor = new HelloWorld ();
- String address = "http://localhost:9000/HelloWorld";
- Endpoint.publish(address, implementor);
- }
- }
查看webservice
在网上查到的一个方法就是通过HttpUrlConnection进行请求,这边贴一下代码,应该很多人都有查到类似的方法
HttpUrlConnection请求实现代码:
- public static void main(String[] args) throws Exception
- {
- String urlString = "http://localhost:9000/HelloWorld?wsdl";//自定义的wsdl服务
- URL url = new URL(urlString);
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();//打开连接
- String xmlFile = "soap_xml\\soap.xml";//要发送的soap格式文件
- File fileToSend = new File(xmlFile);
- byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组
- new FileInputStream(xmlFile).read(buf);
- //Content-Length长度会自动进行计算
- httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
- httpConn.setRequestMethod("POST");
- httpConn.setDoOutput(true);
- httpConn.setDoInput(true);
- OutputStream out = httpConn.getOutputStream();
- out.write(buf);
- out.close();
- InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
- BufferedReader in = new BufferedReader(is);
- String inputLine;
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("result.xml")));// 将结果存放的位置
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println(inputLine);
- bw.write(inputLine);
- bw.newLine();
- }
- bw.close();
- in.close();
- httpConn.disconnect();
- }
soap.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <sayHelloWorldFrom>
- <arg0>
- 撑撑
- </arg0>
- </sayHelloWorldFrom>
- </soap:Body>
- </soap:Envelope>
这段代码是网上找的,并没有错误,但是一运行就懵逼了,报了下面的错误
明明直接在浏览器查看wsdl接口是可以访问页面,但是返回500错误
错误代码:
- Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:9000/HelloWorld?wsdl
- at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
- at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
- at soap.HelloSoap.main(HelloSoap.java:38)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:497)
- at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
错误语句指向
- InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
而网上其他大部分文章都没有给解决方法,或者给了其他的解决方法,我就在这边分享一下这个问题的详细解决方法。
解决流程(干货
首先应该确认具体的错误,通过下面的语句可以了解InputStream的错误详情。
- InputStream is = httpConn.getErrorStream(); //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。
这个语句其实也是从请求的服务方取回的错误信息,实质也是xml内容,用上面的BufferReader那一连串的语句解析出具体内容,然后输出查看,具体代码如下:
- InputStream is = httpConn.getErrorStream(); //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。
- InputStreamReader isr = new InputStreamReader(is,"utf-8");
- BufferedReader in = new BufferedReader(isr);
- String inputLine;
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("result.xml")));// 将结果存放的位置
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println(inputLine);
- bw.write(inputLine);
- bw.newLine();
- bw.close();
- }
- in.close();
错误详情输出如下:
- <?xml version='1.0' encoding='UTF-8'?>
- <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
- <S:Body>
- <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
- <faultcode>
- S:Client
- </faultcode>
- <faultstring>
- 找不到{}sayHelloWorldFrom的分派方法
- </faultstring>
- </S:Fault>
- </S:Body>
- </S:Envelope>
在这边就要注意了!这个xml文件是第一个要点!
分析一下这段xml代码,可以看到有一个faultcode和faultstring,这就是解决这个问题的第一个突破口
灵光一闪百度了一下SOAP Fault(百科连接:http://baike.baidu.com/link?url=vehb23KNtl58uv2cwVDk8LYzDTUC4MHW9kmpaALl9qht9VXp8ASufe0QlpUrEELEApdKx80AMPzMsfCbUJtWiK)
这样就一目了然了,错误代码中faultcode所含的是Client,说明传递的消息有误,服务端是没有问题的。
于是从要发送的soap中查找错误。
这时候发现faultstring “{}找不到xxx的分派方法” 中有一个大括号,想不明白那个是什么,刚好查看了一下web服务的页面,就是最上面那张图片,发现服务名和端口名那边也有大括号{http://example/},然后也想起来之前看的其他xml代码中,要发送的方法的那个节点中有写命名空间(xmlns),就是网上那个getWeatherByCityName的那个例子,大家应该都有看过,我就不再这边费口舌了。
要是有仔细看我发送的soap.xml的代码,就可以看到sayHelloWorldFrom那个节点没有写命名空间,实在是查了这么多代码网上都没人提到,也没有查到关于soap报文的编写规范,还以为那边可以不用写,于是删掉了。
问题就出在这,我重新试了一下,先把命名空间随便写了个网址,再次运行后依然报错,但是大括号中就有了命名空间指向的网址了,于是把web服务里的大括号里的地址,即{http://example/}写上去,总算是请求成功并成功返回数据!
修改后的soap.xml(就是添加了命名空间)
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <m:sayHelloWorldFrom xmlns:m="http://example/">
- <arg0>
- 撑撑
- </arg0>
- </m:sayHelloWorldFrom>
- </soap:Body>
- </soap:Envelope>
大括号里的地址其实就是wsdl中denfinition里定义的targetNameSpace,具体解释我贴个链接吧大家自己看吧,反正知道问题出在这边总算是解决了。
WebService 之 WSDL文件 讲解 http://blog.itpub.net/20200170/viewspace-740276/
其实出了bug第一反应确实应该是找Error信息,这样才好针对性的进行处理,如果本文的方法还没办法帮你解决的话,那还是建议大家靠前面查看错误详情的方法去进行bug的查找~
主要内容到这边结束啦,希望能帮到大家~
附录
- package soap;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Created by cc on 2016/10/21.
- */
- public class HelloSoap {
- public static void main(String[] args) throws Exception
- {
- String urlString = "http://localhost:9000/HelloWorld?wsdl";//wsdl文档的地址
- URL url = new URL(urlString);
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();//打开连接
- //String soapActionString = "http://localhost:9000/HelloWorld/sayHelloWorldFrom";//Soap 1.1中使用
- String xmlFile = "soap_xml\\soap.xml";//要发送的soap格式文件
- File fileToSend = new File(xmlFile);
- byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组
- new FileInputStream(xmlFile).read(buf);
- //Content-Length长度会自动进行计算
- httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
- //httpConn.setRequestProperty("soapActionString",soapActionString);//Soap1.1使用 其实完全可以不需要
- httpConn.setRequestMethod("POST");
- httpConn.setDoOutput(true);
- httpConn.setDoInput(true);
- OutputStream out = httpConn.getOutputStream();
- out.write(buf);
- out.close();
- if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
- {
- InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
- BufferedReader in = new BufferedReader(is);
- String inputLine;
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("result.xml")));// 将结果存放的位置
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println(inputLine);
- bw.write(inputLine);
- bw.newLine();
- }
- bw.close();
- in.close();
- }
- else{
- //如果服务器返回的HTTP状态不是HTTP_OK,则表示发生了错误,此时可以通过如下方法了解错误原因。
- InputStream is = httpConn.getErrorStream(); //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。
- InputStreamReader isr = new InputStreamReader(is,"utf-8");
- BufferedReader in = new BufferedReader(isr);
- String inputLine;
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("result.xml")));// 将结果存放的位置
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println(inputLine);
- bw.write(inputLine);
- bw.newLine();
- bw.close();
- }
- in.close();
- }
- httpConn.disconnect();
- }
- }
转载于:https://www.cnblogs.com/toSeeMyDream/p/6815857.html
java使用POST发送soap报文请求webservice返回500错误解析相关推荐
- python发送soap报文_python用http发送soap报文进行webservice接口调用
最近学习了python用http发送soap报文进行webservice接口调用,从网上找了些资料,为了方便下次温习,在此留下代码片段,也望高手指点: #!/usr/bin/env python # ...
- Java工作笔记-发送SOAP协议请求
这里搭建WebService采用JDKService那种最简单的方式,在此博文中不再说明. 以前说过,调用javaw的API封包是这样的: 这里模拟下. 客户端结构如下: 源码如下: Main2.ja ...
- 设置maxJsonLength,解决ajax通过POST方式调用.net的webService时,数据过长时服务器返回500错误的问题
设置maxJsonLength,解决ajax通过POST方式调用.net的webService时,数据过长时服务器返回500错误的问题 参考文章: (1)设置maxJsonLength,解决ajax通 ...
- 微信小程序访问WebService接口返回500错误解决过程
背景: 新手,第一次尝试使用微信小程序访问WebService接口. 使用Microsoft Visual Studio新建了一个WebService项目,发布后,按照常规流程部署到I ...
- ajax post 请求数据服务器返回500错误
一 .问题描述: 转移一个程序到服务器上,之前的服务器一切正常,今天客户反应内容的分类不显示了,进入后台发现post请求返回500错误 二.解决方法: 报错示例代码: $.ajax({type:&qu ...
- WebService soap报文请求与响应报文解析
需求 今日公司要做一个协同办公系统(OA),PC端已经完成.现在要做一个手机端网页端的.从登陆入手,需要向 服务端发送一段请求报文获取响应报文,对响应报文进行解析判断是否登录成功. 当然手机客户端发送 ...
- Java 用HTTP的方式发送JSON报文请求
前言: 项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接.Socket为长连接:通常情况下S ...
- python发送soap报文_使用Python将带附件的XML发送到SOAP ws
在过去的几周里,我一直在学习Python,并尝试将自定义XML发送到公共测试WS . 现在我觉得我没有取得任何进展 . 所以我现在需要帮助或任何建议 . 如果你使用SoapUI或其他方法(我试过-mz ...
- java post请求返回500错误_Ajax请求Json数据,报500错误,后台没有错误日志。
post请求:http://localhost:9080/DataDiscoveryWeb/issueformcount/queryIssueTendencyDetail.xhtml?jobId=86 ...
最新文章
- 亿级系统的Redis缓存如何设计???
- Nvidia真的收购Arm了吗?
- Angular-cli生成组件修改css成less或sass
- 3.STM32中对EXTI_PE5_Config()函数的理解(自定义)之中断控制按键LED
- axure9 html文件使用ie打开图片无法显示_win7系统html文件如何打开 win7系统html文件打开方法【介绍】...
- idea+maven下jrebel的安装破解
- servlet返回数据给html_Servlet 简介
- 用python做一个简单的投票程序_以一个投票程序的实例来讲解Python的Django框架使...
- 德国力挺华为:建5G网络不排除任何设备厂商
- 华视读卡器多浏览器插件_翻遍Chrome商店,这9款插件值得安装
- java标签不显示文字_此程序在运行后,窗体上不显示标签的文字,也不显示图标,我自己检查也没发现什么问题,请大神帮帮忙看看我哪个地方有问题?...
- Android应用程序访问linux驱动第二步:实现并测试hardware层
- vscode 折叠/展开所有区域代码快捷键
- H265 CTU、CU、PU、TU划分的特点及要求
- 软件工程的6个阶段以及成果精简版
- 华为交换机console口如何设置密码
- win10下java的下载、安装和配置环境教程,超级详细
- iOS 手机淘宝 自动创建一个人的群聊 实现源码 hook 代码源码
- linux查看usb文件,linux lsusb查看USB信息
- 网页导出pdf不完整_网页文本无法复制?学会这3个套路,一分钟帮你突破限制...
热门文章
- 为什么现在还有很多人喜欢在银行存定期?
- 每天晚上坚持喝一杯无糖燕麦会怎样?
- 生意人没有“攀比”的目标,会过的很迷茫
- Since WWDC released the first developer
- Windows下Redis的启动命令
- SQL Server中的表变量
- ssis sql_SSIS OLE DB来源:SQL命令与表或视图
- sql用于字符串的聚合函数_SQL字符串函数用于数据整理(争用)
- sql server表分区_介绍分区表SQL Server增量统计信息
- SQL Server数据库迁移–将数据库克隆到另一个排序规则