如何在JAVA中传递SOAP请求属性(如SoapUI中所示)

时间:2018-07-27 10:15:56

标签: java soap

SOAP UI中的某些属性必须切换为true才能在请求中传递文件。 它是启用内联文件。 https://www.soapui.org/docs/soap-and-wsdl/attachments.html

我已经构造了一个Soap请求并将其成功拉到Web服务。 soap request

如果将此属性切换为false,soap会给出错误消息“无法读取流”。

好吧,在用Java构建soap请求之后,我遇到了相同的错误,但是所有其他请求都运行良好。

  //CREATE_DOCUMENT
    private static void createDocSoapEnvelope(SOAPMessage soapMessage,String token, String parentID, String name, String comment, boolean advancedVersionControl,Map<String,String> metadata,Map<String,String> attach,String fileName) throws SOAPException 
    {

        SOAPPart soapPart = soapMessage.getSOAPPart();

        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPFactory factory = SOAPFactory.newInstance();

        SOAPHeader soapHeader = envelope.getHeader();
        SOAPBody soapBody = envelope.getBody();

        soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

        //HEADER
        SOAPElement headerElem = 
                factory.createElement(headerName,prefix,namespace);
        SOAPElement subHeaderElem = 
                factory.createElement("AuthenticationToken",prefix,namespace);
        SOAPElement soapHeaderElem;
        SOAPElement soapHeaderElem1;
        soapHeaderElem = soapHeader.addChildElement(headerElem);
        soapHeaderElem1 = soapHeaderElem.addChildElement("AuthenticationToken");
        soapHeaderElem1.addTextNode(token);
        //BODY
        SOAPElement soapBodyElem;
        SOAPElement soapBodyElem1;
        SOAPElement soapBodyElem2;
        SOAPElement soapBodyElem3;
        SOAPElement soapBodyElem4;
        SOAPElement soapBodyElem5;
        SOAPElement soapBodyElem6;
        soapBodyElem =soapBody.addChildElement(serviceName,prefix1,namespace1);
        /*soapBodyElem1=soapBodyElem.addChildElement("id");
        soapBodyElem1.addTextNode("42321");*/
        soapBodyElem1=soapBodyElem.addChildElement("parentID",prefix1,namespace1);
        soapBodyElem1.addTextNode(parentID);
        soapBodyElem2=soapBodyElem.addChildElement("name",prefix1,namespace1);
        soapBodyElem2.addTextNode(name);
        soapBodyElem3=soapBodyElem.addChildElement("comment",prefix1,namespace1);
        soapBodyElem3.addTextNode(comment);
        soapBodyElem4=soapBodyElem.addChildElement("advancedVersionControl",prefix1,namespace1);
        soapBodyElem4.addTextNode(String.valueOf(advancedVersionControl));
        soapBodyElem5=soapBodyElem.addChildElement("metadata",prefix1,namespace1);
            /*SOAPElement subElementM = soapBodyElem5.addChildElement("AttributeGroups");
                SOAPElement subElementAttr1 = subElementM.addChildElement("DisplayName");
                subElementAttr1.addTextNode(metadata.get("DisplayName"));
                SOAPElement subElementAttr2 = subElementM.addChildElement("Key");
                subElementAttr2.addTextNode(metadata.get("Key"));
                SOAPElement subElementAttr3 = subElementM.addChildElement("Type");
                subElementAttr3.addTextNode(metadata.get("Type"));
                SOAPElement subElementAttr4 = subElementM.addChildElement("Values");
                    SOAPElement subElementV1 = subElementM.addChildElement("Description",prefix2,namespace2);
                    subElementV1.addTextNode(metadata.get("Description"));
                    SOAPElement subElementV2 = subElementM.addChildElement("ValueKey",prefix2,namespace2);
                    subElementV2.addTextNode(metadata.get("ValueKey"));*/
        soapBodyElem6=soapBodyElem.addChildElement("attach",prefix1,namespace1);
            SOAPElement subElementA1 = soapBodyElem6.addChildElement("CreatedDate",prefix2,namespace2);
            subElementA1.addTextNode(attach.get("CreatedDate"));
            SOAPElement subElementA2 = soapBodyElem6.addChildElement("FileName",prefix2,namespace2);
            subElementA2.addTextNode(attach.get("FileName"));
            SOAPElement subElementA3 = soapBodyElem6.addChildElement("FileSize",prefix2,namespace2);
            subElementA3.addTextNode(attach.get("FileSize"));
            SOAPElement subElementA4 = soapBodyElem6.addChildElement("ModifiedDate",prefix2,namespace2);
            subElementA4.addTextNode(attach.get("ModifiedDate"));
            SOAPElement subElementA5 = soapBodyElem6.addChildElement("Contents",prefix2,namespace2);
            subElementA5.addTextNode("file:"+fileName);

    }

    private static SOAPMessage createDocSOAPRequest(String namespace,String token, String parentID, String name, String comment, boolean advancedVersionControl,Map<String,String> metadata,Map<String,String> attach,String fileName) throws Exception
    {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createDocSoapEnvelope(soapMessage,token,parentID,name,comment,advancedVersionControl,metadata,attach,fileName);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("Namespace", namespace);

        soapMessage.saveChanges();

        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

    public static String createDocument(String token, String parentID, String name, String comment, boolean advancedVersionControl,Map<String,String> metadata,Map<String,String> attach,String fileName) 
    {
        SOAPConnectionFactory soapFactory = null;
        SOAPConnection soapConnect = null;
        SOAPMessage soapRequest = null;
        SOAPMessage soapResponse = null;
        String response = "";

        try {
            soapFactory = SOAPConnectionFactory.newInstance();
            soapConnect = soapFactory.createConnection();

            soapRequest  = createDocSOAPRequest(namespace,token,parentID,name, comment,advancedVersionControl,metadata,attach,fileName);

            soapResponse = soapConnect.call(soapRequest, soapUrl);

            response = printSOAPMessage (soapResponse);

            soapConnect.close();

            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

如何通过Java添加此请求属性? java soapMessage.setProperty方法获得两个参数,第一个可以是来自SOAPMessage的常量。但是只有两个常量:CHARACTER_SET_ENCODING和 WRITE_XML_DECLARATION。

谢谢

P.S。 尝试在SOAP的http登录中查找传递信息,在How to pass SOAP Request Properties in PHP (as shown in SoapUI)中如何提供 但是肥皂不能通过http传递此参数。

0 个答案:

没有答案