introduction
Before calling the webservice interface of other systems, the client was downloaded directly, and then developed according to the wsdl document
But this time, the interface that calls the erp system needs to set the header. There is no problem with the soapUI test, but there is a problem with the code implementation.
Because it is not written in the envelope, but directly in the header of the request, I tried many methods, and finally realized it, but other attempts may also be used next time, hereby record
HttpClient calling method
public static String postErpStock(String url, String pOrganizatoinCode, String pDataType) throws Exception {
// 定义httpClient的实例
CloseableHttpClient httpclient = HttpClients.createDefault();
String retVal;
try {
HttpPost httpPost = new HttpPost(url);
String wsdlData=
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:inv=\"http://com/oracle/apps/invqty/Invqty.wsdl\" xmlns:typ=\"http://com/oracle/apps/invqty/Invqty.wsdl/types/\">"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+"<inv:getInvQtyInfo>"
+" <xReturnStatus_out></xReturnStatus_out>"
+" <xMsgCount_out></xMsgCount_out>"
+" <xMsgData_out></xMsgData_out>"
+" <xInvQtyInfo_out>"
+"<typ:InvqtyInvQtyRecUser>"
+" <typ:locator></typ:locator>"
+"<typ:totalQuantity></typ:totalQuantity>"
+" <typ:ouOrgId></typ:ouOrgId>"
+" <typ:itemType></typ:itemType>"
+"<typ:att></typ:att>"
+"<typ:organizationId></typ:organizationId>"
+"<typ:itemId></typ:itemId>"
+" <typ:organizationCode></typ:organizationCode>"
+" <typ:dataType></typ:dataType>"
+" <typ:itemDescription></typ:itemDescription>"
+" <typ:atr></typ:atr>"
+" <typ:subinventoryCode></typ:subinventoryCode>"
+" <typ:itemNumber></typ:itemNumber>"
+" </typ:InvqtyInvQtyRecUser>"
+" </xInvQtyInfo_out>"
+" <pOrganizatoinCode>"+pOrganizatoinCode+"</pOrganizatoinCode>"
+" <pDataType>"+pDataType+"</pDataType>"
+" <pItemNumber></pItemNumber>"
+" </inv:getInvQtyInfo>"
+" </soapenv:Body>"
+"</soapenv:Envelope>";
StringEntity myEntity = new StringEntity(wsdlData,
ContentType.create("text/xml", "UTF-8"));
httpPost.setEntity(myEntity);
httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// 伪装一个浏览器
httpPost.setHeader("LoginCode","123");
httpPost.setHeader("LoginPassword","123");
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
HttpEntity entity2 = (HttpEntity) response2.getEntity();
if (entity2 != null) {
retVal = EntityUtils.toString(entity2, "UTF-8");
} else {
retVal = null;
}
EntityUtils.consume(entity2);
return retVal;
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
Reference blog: https://blog.csdn.net/myfmyfmyfmyf/article/details/54342671
other attempts
1. Create a service from scratch
String wsdlUrl = "http://IP:9099/org.URL/enh/invqty?WSDL";
String namespaceUrl = "http://com/URL/Invqty.wsdl";
// 1.创建服务
Holder<String> xReturnStatusOut = new Holder<String>();
xReturnStatusOut.value = "";
Holder<BigDecimal> xMsgCountOut = new Holder<BigDecimal>();
xMsgCountOut.value = new BigDecimal("0");
Holder<String> xMsgDataOut = new Holder<String>();
xMsgDataOut.value = "";
InvqtyInvQtyRecUserArray invqtyInvQtyRecUserArray = new InvqtyInvQtyRecUserArray();
invqtyInvQtyRecUserArray.getInvqtyInvQtyRecUser().add(new InvqtyInvQtyRecUser());
Holder<InvqtyInvQtyRecUserArray> xInvQtyInfoOut = new Holder<InvqtyInvQtyRecUserArray>();
xInvQtyInfoOut.value = invqtyInvQtyRecUserArray;
Invqty_Service invqty_Service = new Invqty_Service();
Invqty invqty = invqty_Service.getInvqtyPort();
// 2.创建Dispatch
Dispatch<SOAPMessage> dispatch = invqty_Service.createDispatch(
new QName("http://com/URL/Invqty.wsdl", "invqtyPort"), SOAPMessage.class,
Service.Mode.MESSAGE);
// 3.创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
// header信息
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = envelope.addHeader();
}
QName LoginCode = new QName(namespaceUrl, "LoginCode");
QName LoginPassword = new QName(namespaceUrl, "LoginPassword");
QName ClientId = new QName(namespaceUrl, "ClientId");
QName OperationCode = new QName(namespaceUrl, "OperationCode");
header.addHeaderElement(LoginCode).setValue("12");
header.addHeaderElement(LoginPassword).setValue("12");
header.addHeaderElement(ClientId).setValue("12");
header.addHeaderElement(OperationCode).setValue("12");
// 4.创建QName来指定消息中传递的数据
QName ename = new QName(namespaceUrl, "list", "ns");
body.addBodyElement(ename);
msg.writeTo(System.out);
System.out.println();
// 5.通过Distpatch传递信息
SOAPMessage responseMsg = dispatch.invoke(msg);
responseMsg.writeTo(System.out);
System.out.println();
invqty.getInvQtyInfo(xReturnStatusOut, xMsgCountOut, xMsgDataOut, xInvQtyInfoOut, "11", "12", "");
Reference blog: https://blog.csdn.net/zhutulang/article/details/51125897
2. Set the HandlerResolver method
part of the code
SOAPElement LoginCode = header.addChildElement("LoginCode");
LoginCode.addTextNode("");
SOAPElement LoginPassword = header.addChildElement("LoginPassword");
LoginPassword.addTextNode("");
Reference blog: https://blog.csdn.net/East271536394/article/details/6699222
3. Set the SOAPHeader method
SOAPHeader header = envelope.getHeader();
if(header == null){
header = envelope.addHeader();
}
QName LoginCode = new QName(namespaceUrl, "LoginCode", "ns");
QName LoginPassword = new QName(namespaceUrl, "LoginPassword", "ns");
header.addHeaderElement(LoginCode).setValue("444"); header.addHeaderElement(LoginPassword).setValue("123456");
header.addAttribute(new QName("LoginCode"), "enh_ws");
header.addAttribute(new QName("LoginPassword"), "123456");
header.setAttribute("LoginCode", "enh_ws");
header.setAttribute("LoginPassword", "123456");
None of the above will work, probably because the header is not in the envelope, but in the request
summary
I started to wonder why soapUI works and why the code doesn't work. After some research on soap, I
ended up imitating the browser to make calls like soapUI, so I could get data
. The attempts during this period also deepened my understanding of webservice.
Reference blog:
https://blog.csdn.net/myfmyfmyfmyf/article/details/54342671
https://www.cnblogs.com/lanxuezaipiao/archive/2013/05/10/3072216.html
https://blog.csdn .net/oscar999/article/details/40340819
https://blog.csdn.net/zhutulang/article/details/51125897
https://blog.csdn.net/zsj65776529/article/details/55510753?locationNum=13&fps=1
http://liuxueping1987.iteye.com/blog/1600651
http://www.blogjava.net/xxgshxs/archive/2012/08/14/385442.html