当前位置 : 主页 > 网页制作 > Nodejs >

【Webservice】客户端调用设置header

来源:互联网 收集:自由互联 发布时间:2021-06-24
引言 之前调用其它系统的webservice接口,都是直接下载客户端,然后根据wsdl文档进行开发 但是这次调用erp系统的接口需要设置header,本来才soapUI测试没什么问题,但是用代码实现就出了

引言

之前调用其它系统的webservice接口,都是直接下载客户端,然后根据wsdl文档进行开发

但是这次调用erp系统的接口需要设置header,本来才soapUI测试没什么问题,但是用代码实现就出了问题

因为它不是写在envelope里面,而是直接在请求中的header中,尝试了很多方法,最终实现了,不过其他的尝试或许下次也可以用,特此记录

 

HttpClient调用方式

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();
        }
    }

参考博客:https://blog.csdn.net/myfmyfmyfmyf/article/details/54342671

 

其他的尝试

1.从头开始创建服务

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", "");

参考博客:https://blog.csdn.net/zhutulang/article/details/51125897

 

2.设置HandlerResolver方法

部分代码

SOAPElement LoginCode = header.addChildElement("LoginCode");
LoginCode.addTextNode("");
SOAPElement LoginPassword = header.addChildElement("LoginPassword");
LoginPassword.addTextNode("");

参考博客:https://blog.csdn.net/East271536394/article/details/6699222

 

3.设置SOAPHeader方法

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");

以上都不行,原因大概是因为header不在envelope里面,而是在请求中

 

小结

开始在想为什么soapUI可以,为什么代码不可以,研究了一番soap之后
最后还是向soapUI一样,直接模仿浏览器进行调用,这样就可以获取数据
期间的尝试也也加深了自己对webservice的了解

 
 
 

参考博客:
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

网友评论