webservice post请求,java发布webservice接口

  webservice post请求,java发布webservice接口

  

目录

职位请求服务服务端1.例如我此时有一个服务描述语言文件2.点击排查看具体的发送参数3.代码实现3.1参数说明用邮政请求调用服务

 

  

post请求webservice服务端

当生成服务的客户端不好实现时,通过爪哇岛的邮政请求不失为一种好办法。

 

  

1.例如我此时有一个wsdl文件

http://XXX。XXX。XXX。xxx33608081/API服务。SVC?wsdl2 .通过调试工具我们可以将工作流定义语言文件转换。从而模拟发送请求。以及参数

 

  (不懂索普伊请自行百度)

  

2.点击row查看具体的发送参数

 

  具体参数对应规则(以下对应代码上应该发送的参数是什么)

  

3.代码实现

public static void main(String[]args){ try { String URL= http://XXX。XXX。XXX。xxx33608081/API服务。SVC ;string params= soap env : envelope xmlns : soap env= http://架构。XML soap。org/soap/envelope/ xmlns : tem= http://tempuri。org/ xmlns : HN= http://模式。数据契约。HN .数据访问。bll n /tem : input n /soap env : body n /soap env : envelope ;字符串host= XXX。XXX。XXX。xxx33608081 ;string soap action= http://AAA/AAA/AAA ;//接受返回报文字符串结果=新字符串();URL u=新网址(网址);http URL connection conn=(HttpURLConnection)u . open connection();conn . setdoinput(true);//允许对外输出数据conn . setdoooutput(true);conn . setusecaches(false);conn . setdefaultusecaches(false);conn.setRequestProperty(Host ,Host);conn . setrequestproperty( Content-Type , text/XML;charset=UTF-8 );//soap conn . setrequestproperty( soap action ,soap action);conn . setrequestproperty( Content-Length ,字符串。(参数的值。length()));conn . setrequestmethod( POST );//定义输出流输出流output=conn . get输出流();if(字符串实用程序。isnotblank(params)){ byte[]b=params。getbytes( UTF-8 );//发送肥皂请求报文输出.写入(b,0,b。长度);输出。flush();输出。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

  

//请求头必须设置SOAPActionconnection.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(); } } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: