java如何实现post请求webservice服务端
目录
- post请求webservice服务端
- 1.例如我此时有一个wsdl文件
- 2.点击row查看具体的发送参数
- 3.代码实现
- 3.1参数说明
- 用post请求调用webservice
post请求webservice服务端
当生成webService的客户端不好实现时,通过java的post请求不失为一种好办法。
1.例如我此时有一个wsdl文件
http://xxx.xxx.xxx.xxx:8081/APIService.svc?wsdl
2.通过SoapUI 我们可以将swdl文件转换。从而模拟发送请求。以及参数
(不懂soapui请自行百度)
2.点击row查看具体的发送参数
具体参数对应规则(以下对应代码上应该发送的参数是什么)
3.代码实现
public static void main(String[] args) { try{ String url="http://xxx.xxx.xxx.xxx:8081/APIService.svc"; String params="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:hn=\"http://schemas.datacontract.org/2004/07/hn.DataAccess.bll\">\n" + " </tem:input>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; String host="xxx.xxx.xxx.xxx:8081"; String SOAPAction="http://aaa/aaa/aaa"; //接受返回报文 String result = new String(); URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoInput(true); //允许对外输出数据 conn.setDoOutput(true); conn.setUseCaches(false); conn.setDefaultUseCaches(false); conn.setRequestProperty("Host",host); conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8"); //soap conn.setRequestProperty("SOAPAction",SOAPAction); conn.setRequestProperty("Content-Length",String.valueOf(params.length())); conn.setRequestMethod("POST"); //定义输出流 OutputStream output = conn.getOutputStream(); if(StringUtils.isNotBlank(params)){ byte[] b = params.getBytes("UTF-8"); //发送soap请求报文 output.write(b,0,b.length); output.flush(); output.close(); //定义输入流,获取soap报文 InputStream input = conn.getInputStream(); //设置编码格式 result = IOUtils.toString(input,"UTF-8"); input.close(); } System.out.println("请求返回报文:" + result); }catch (Exception e){ System.out.println(e.getMessage()); } }
3.1参数说明
url
:这个地址即wsdl文件地址去掉后缀 “?wsdl”。如此例就是:wsdl
:
http://xxx.xxx.xxx.xxx:8081/APIService.svc?wsdl
url
:
http://xxx.xxx.xxx.xxx:8081/APIService.svc
params
:这个即是请求的参数host
:这个是对方的主机地址和端口号。 如此例为: xxx.xxx.xxx.xxx:8081SOAPAction
:具体service路径
用post请求调用webservice
先说下遇到的坑,最先用post请求的时候一直返回500的错误码
最终通过打印错误信息分析,请求头必须添加SOAPAction
//请求头必须设置SOAPAction connection.setRequestProperty("SOAPAction","application/soap+xml; charset=utf-8");
xml转json需要用到hutool-all-4.0.12.jar,解析json比xml要简单得多。
如果不需要用xml转json,除需要jdk自带的包不再需要引入其他的包。
package com.baidu.util; import cn.hutool.json.XML; import net.sf.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * @author 蓝珂一剑 * @version 1.0.0 * @ClassName SoapUtil.java * @Description webservice调用工具类 * @createTime 2020/9/29 10:37 */ public class SoapUtil { /** * 发送http post调用webservice * * @param strUrl * @param requestSoapXml * @return * @throws IOException */ public static String soapPost(String strUrl, String requestSoapXml) throws IOException { //第一步:创建服务地址,不是WSDL地址 URL url = new URL(strUrl); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST必须大写 connection.setRequestMethod("POST"); //3.2设置数据格式:content-type connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); connection.setDoOutput(true); //请求头必须设置SOAPAction connection.setRequestProperty("SOAPAction","application/soap+xml; charset=utf-8"); //第四步:组织SOAP数据,发送请求 OutputStream os = connection.getOutputStream(); //os.write(requestSoapXml.getBytes());这个方法遇到一个大坑记录下 //当时在开发工具中通过这个方法调用接口一切都OK,但是放到客户服务器上发送post //请求就返回500错误,错误信息太少,走了很多弯路,百度无果后, //后面灵机一动,会不会是传输的数据编码不对, //导致接口服务端解析请求数据报错从而返回500错误码, //其实我是不知道服务端到底用的什么编码, //经过尝试 os.write(requestSoapXml.getBytes("utf-8")); //结果成功了,根据实际情况这个编码会改动 os.write(requestSoapXml.getBytes()); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); //==============打印错误信息 // try{ // InputStream iss = connection.getErrorStream(); //通过getErrorStream了解错误的详情 // InputStreamReader isrs = new InputStreamReader(iss,"utf-8"); // BufferedReader ins = new BufferedReader(isrs); // String inputLine; // BufferedWriter bws = new BufferedWriter(new OutputStreamWriter( // new FileOutputStream("result.xml")));// 将结果存放的位置 // while ((inputLine = ins.readLine()) != null) // { // System.out.println(inputLine); // bws.write(inputLine); // bws.newLine(); // // } // bws.close(); // }catch (Exception e){ // // } //============== StringBuilder sb = new StringBuilder(); if (200 == responseCode) {//表示服务端响应成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String temp = null; while (null != (temp = br.readLine())) { sb.append(temp); } is.close(); isr.close(); br.close(); }else{ throw new RuntimeException("调用webservice失败:服务器端返回HTTP code " + responseCode + "信息:" ); } os.close(); return sb.toString(); } public static void main(String[] args) { String url = "http://192.168.100.86:6888/ormrpc/services/WSImpExtFaCardFacade"; String p = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.app.manage.fa.fi.eas.kingdee.com\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <web:impFaCard soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" + " <jsonStr xsi:type=\"xsd:string\">{\"header\":{\"bizcode\":\"42AC39EC\",\"bizdate\":\"2020-10-12\"},\"datas\":[{\"companynum\":\"D01\",\"facatnum\":\"04\",\"number\":\"JD04202010120000175\",\"oldnumber\":\"\",\"assetname\":\"服务器\",\"assetamount\":\"1\",\"measureunitnum\":\"件\",\"recorddate\":\"2020-10-12\",\"fiaccountdate\":\"2020-10-12\",\"originnum\":\"00101\",\"fausestatusnum\":\"101\",\"economypurposenum\":\"001\",\"storecitynum\":\"001\",\"specs\":\" \",\"parea\":\"\",\"manufacturer\":\"\",\"leavefactorydate\":\"\",\"paperno\":\"\",\"fromunit\":\"-1\",\"deptnum\":\"JD.001\",\"keepernum\":\"EP17019484\",\"remark\":\"\",\"currencynum\":\"BB01\",\"exchangerate\":\"1\",\"originalamount\":\"12000.0\",\"additiveamount\":\"0\",\"localamount\":\"12000.0\",\"inputtax\":\"0\",\"fairvalue\":\"0\",\"buyoriginalvalue\":\"0\",\"buyaddupdep\":\"0\",\"treatmentincome\":\"0\",\"deliverdate\":\"\",\"startdate\":\"2020-10-12\",\"expuseyears\":\"5\",\"expuseperiod\":\"60\",\"depperiod\":\"0\",\"addupdep\":\"0\",\"neatleftrate\":\"0\",\"expneatleftvalue\":\"3600.0\",\"neatvalue\":\"12000.0\",\"addupdepthisyear\":\"0\",\"decprepare\":\"0\",\"neatamount\":\"12000.0\",\"deprmethodnum\":\"0\",\"measureunitwlnum\":\"\",\"accountassetsnum\":\"04\",\"accountaccudeprnum\":\"04\",\"accountdecvaluenum\":\"04\",\"initevalvalue\":\"0\",\"evalperiodleft\":\"0\",\"accudeprall\":\"0\",\"hasdisabled\":\"0\",\"addonfare\":\"0\",\"monthrate\":\"0\",\"wrtyperiod\":\"\",\"wrtynumber\":\"\",\"groupnumber\":\"\",\"barcode\":\"JD04202010120000175\",\"usedepartmentnum\":\"JD.001\",\"usepersonnum\":\"\",\"usage\":\"\",\"asstacttypenum\":\"CBZX\",\"asstactitemnum\":\"Y0101\",\"accountnum\":\"6602.05\",\"apportionscale\":\"100\"},{\"companynum\":\"D01\",\"facatnum\":\"04\",\"number\":\"JD04202010120000176\",\"oldnumber\":\"\",\"assetname\":\"服务器\",\"assetamount\":\"1\",\"measureunitnum\":\"件\",\"recorddate\":\"2020-10-12\",\"fiaccountdate\":\"2020-10-12\",\"originnum\":\"00101\",\"fausestatusnum\":\"101\",\"economypurposenum\":\"001\",\"storecitynum\":\"001\",\"specs\":\" \",\"parea\":\"\",\"manufacturer\":\"\",\"leavefactorydate\":\"\",\"paperno\":\"\",\"fromunit\":\"-1\",\"deptnum\":\"JD.001\",\"keepernum\":\"EP17019484\",\"remark\":\"\",\"currencynum\":\"BB01\",\"exchangerate\":\"1\",\"originalamount\":\"12000.0\",\"additiveamount\":\"0\",\"localamount\":\"12000.0\",\"inputtax\":\"0\",\"fairvalue\":\"0\",\"buyoriginalvalue\":\"0\",\"buyaddupdep\":\"0\",\"treatmentincome\":\"0\",\"deliverdate\":\"\",\"startdate\":\"2020-10-12\",\"expuseyears\":\"5\",\"expuseperiod\":\"60\",\"depperiod\":\"0\",\"addupdep\":\"0\",\"neatleftrate\":\"0\",\"expneatleftvalue\":\"3600.0\",\"neatvalue\":\"12000.0\",\"addupdepthisyear\":\"0\",\"decprepare\":\"0\",\"neatamount\":\"12000.0\",\"deprmethodnum\":\"0\",\"measureunitwlnum\":\"\",\"accountassetsnum\":\"04\",\"accountaccudeprnum\":\"04\",\"accountdecvaluenum\":\"04\",\"initevalvalue\":\"0\",\"evalperiodleft\":\"0\",\"accudeprall\":\"0\",\"hasdisabled\":\"0\",\"addonfare\":\"0\",\"monthrate\":\"0\",\"wrtyperiod\":\"\",\"wrtynumber\":\"\",\"groupnumber\":\"\",\"barcode\":\"JD04202010120000176\",\"usedepartmentnum\":\"JD.001\",\"usepersonnum\":\"\",\"usage\":\"\",\"asstacttypenum\":\"CBZX\",\"asstactitemnum\":\"Y0101\",\"accountnum\":\"6602.05\",\"apportionscale\":\"100\"},{\"companynum\":\"D01\",\"facatnum\":\"04\",\"number\":\"JD04202010120000177\",\"oldnumber\":\"\",\"assetname\":\"服务器\",\"assetamount\":\"1\",\"measureunitnum\":\"件\",\"recorddate\":\"2020-10-12\",\"fiaccountdate\":\"2020-10-12\",\"originnum\":\"00101\",\"fausestatusnum\":\"101\",\"economypurposenum\":\"001\",\"storecitynum\":\"001\",\"specs\":\" \",\"parea\":\"\",\"manufacturer\":\"\",\"leavefactorydate\":\"\",\"paperno\":\"\",\"fromunit\":\"-1\",\"deptnum\":\"JD.001\",\"keepernum\":\"EP17019484\",\"remark\":\"\",\"currencynum\":\"BB01\",\"exchangerate\":\"1\",\"originalamount\":\"12000.0\",\"additiveamount\":\"0\",\"localamount\":\"12000.0\",\"inputtax\":\"0\",\"fairvalue\":\"0\",\"buyoriginalvalue\":\"0\",\"buyaddupdep\":\"0\",\"treatmentincome\":\"0\",\"deliverdate\":\"\",\"startdate\":\"2020-10-12\",\"expuseyears\":\"5\",\"expuseperiod\":\"60\",\"depperiod\":\"0\",\"addupdep\":\"0\",\"neatleftrate\":\"0\",\"expneatleftvalue\":\"3600.0\",\"neatvalue\":\"12000.0\",\"addupdepthisyear\":\"0\",\"decprepare\":\"0\",\"neatamount\":\"12000.0\",\"deprmethodnum\":\"0\",\"measureunitwlnum\":\"\",\"accountassetsnum\":\"04\",\"accountaccudeprnum\":\"04\",\"accountdecvaluenum\":\"04\",\"initevalvalue\":\"0\",\"evalperiodleft\":\"0\",\"accudeprall\":\"0\",\"hasdisabled\":\"0\",\"addonfare\":\"0\",\"monthrate\":\"0\",\"wrtyperiod\":\"\",\"wrtynumber\":\"\",\"groupnumber\":\"\",\"barcode\":\"JD04202010120000177\",\"usedepartmentnum\":\"JD.001\",\"usepersonnum\":\"\",\"usage\":\"\",\"asstacttypenum\":\"CBZX\",\"asstactitemnum\":\"Y0101\",\"accountnum\":\"6602.05\",\"apportionscale\":\"100\"},{\"companynum\":\"D01\",\"facatnum\":\"04\",\"number\":\"JD04202010120000178\",\"oldnumber\":\"\",\"assetname\":\"服务器\",\"assetamount\":\"1\",\"measureunitnum\":\"件\",\"recorddate\":\"2020-10-12\",\"fiaccountdate\":\"2020-10-12\",\"originnum\":\"00101\",\"fausestatusnum\":\"101\",\"economypurposenum\":\"001\",\"storecitynum\":\"001\",\"specs\":\" \",\"parea\":\"\",\"manufacturer\":\"\",\"leavefactorydate\":\"\",\"paperno\":\"\",\"fromunit\":\"-1\",\"deptnum\":\"JD.001\",\"keepernum\":\"EP17019484\",\"remark\":\"\",\"currencynum\":\"BB01\",\"exchangerate\":\"1\",\"originalamount\":\"12000.0\",\"additiveamount\":\"0\",\"localamount\":\"12000.0\",\"inputtax\":\"0\",\"fairvalue\":\"0\",\"buyoriginalvalue\":\"0\",\"buyaddupdep\":\"0\",\"treatmentincome\":\"0\",\"deliverdate\":\"\",\"startdate\":\"2020-10-12\",\"expuseyears\":\"5\",\"expuseperiod\":\"60\",\"depperiod\":\"0\",\"addupdep\":\"0\",\"neatleftrate\":\"0\",\"expneatleftvalue\":\"3600.0\",\"neatvalue\":\"12000.0\",\"addupdepthisyear\":\"0\",\"decprepare\":\"0\",\"neatamount\":\"12000.0\",\"deprmethodnum\":\"0\",\"measureunitwlnum\":\"\",\"accountassetsnum\":\"04\",\"accountaccudeprnum\":\"04\",\"accountdecvaluenum\":\"04\",\"initevalvalue\":\"0\",\"evalperiodleft\":\"0\",\"accudeprall\":\"0\",\"hasdisabled\":\"0\",\"addonfare\":\"0\",\"monthrate\":\"0\",\"wrtyperiod\":\"\",\"wrtynumber\":\"\",\"groupnumber\":\"\",\"barcode\":\"JD04202010120000178\",\"usedepartmentnum\":\"JD.001\",\"usepersonnum\":\"\",\"usage\":\"\",\"asstacttypenum\":\"CBZX\",\"asstactitemnum\":\"Y0101\",\"accountnum\":\"6602.05\",\"apportionscale\":\"100\"},{\"companynum\":\"D01\",\"facatnum\":\"04\",\"number\":\"JD04202010120000179\",\"oldnumber\":\"\",\"assetname\":\"服务器\",\"assetamount\":\"1\",\"measureunitnum\":\"件\",\"recorddate\":\"2020-10-12\",\"fiaccountdate\":\"2020-10-12\",\"originnum\":\"00101\",\"fausestatusnum\":\"101\",\"economypurposenum\":\"001\",\"storecitynum\":\"001\",\"specs\":\" \",\"parea\":\"\",\"manufacturer\":\"\",\"leavefactorydate\":\"\",\"paperno\":\"\",\"fromunit\":\"-1\",\"deptnum\":\"JD.001\",\"keepernum\":\"EP17019484\",\"remark\":\"\",\"currencynum\":\"BB01\",\"exchangerate\":\"1\",\"originalamount\":\"12000.0\",\"additiveamount\":\"0\",\"localamount\":\"12000.0\",\"inputtax\":\"0\",\"fairvalue\":\"0\",\"buyoriginalvalue\":\"0\",\"buyaddupdep\":\"0\",\"treatmentincome\":\"0\",\"deliverdate\":\"\",\"startdate\":\"2020-10-12\",\"expuseyears\":\"5\",\"expuseperiod\":\"60\",\"depperiod\":\"0\",\"addupdep\":\"0\",\"neatleftrate\":\"0\",\"expneatleftvalue\":\"3600.0\",\"neatvalue\":\"12000.0\",\"addupdepthisyear\":\"0\",\"decprepare\":\"0\",\"neatamount\":\"12000.0\",\"deprmethodnum\":\"0\",\"measureunitwlnum\":\"\",\"accountassetsnum\":\"04\",\"accountaccudeprnum\":\"04\",\"accountdecvaluenum\":\"04\",\"initevalvalue\":\"0\",\"evalperiodleft\":\"0\",\"accudeprall\":\"0\",\"hasdisabled\":\"0\",\"addonfare\":\"0\",\"monthrate\":\"0\",\"wrtyperiod\":\"\",\"wrtynumber\":\"\",\"groupnumber\":\"\",\"barcode\":\"JD04202010120000179\",\"usedepartmentnum\":\"JD.001\",\"usepersonnum\":\"\",\"usage\":\"\",\"asstacttypenum\":\"CBZX\",\"asstactitemnum\":\"Y0101\",\"accountnum\":\"6602.05\",\"apportionscale\":\"100\"}]}</jsonStr>\n" + " </web:impFaCard>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; try{ String response = SoapUtil.soapPost(url,p); cn.hutool.json.JSONObject data = XML.toJSONObject(response); //从data中解析rescode和resmsg,其中"rescode"为0时代表成功,其他均为失败。 String resMsg = data.getJSONObject("soapenv:Envelope").getJSONObject("soapenv:Body").getJSONObject("ns1:impFaCardResponse").getJSONObject("impFaCardReturn").getStr("content"); JSONObject jsonObject = JSONObject.fromObject(resMsg); String bizcode = jsonObject.getString("bizcode"); String bizdate = jsonObject.getString("bizdate"); String rescode = jsonObject.getString("rescode"); String resmsg = jsonObject.getString("resmsg"); System.out.println(resmsg); }catch (Exception e){ e.printStackTrace(); } } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)