如何在SOAP POST请求Web服务中与邮件正文一起使用附件?

时间:2019-04-16 05:24:44

标签: java post attachment

我想打SOAP POST请求Web服务,例如说http://localhost:8080/someService

我在xml中有请求正文,

现在,我还有一个附件作为文本文件存在于某些C:drive中 再说C:/Desktop/test.txt。

任何人都可以告诉我Java的实现。我尝试使用 HTTPURlConnection和SOAPCOnnection,但我无法使其正常工作。

            String responseString = "";
    String outputString = "";
    String wsURL = "http://10.123.10.15:1234/ABCDEF";
    URL url = new URL(wsURL);
    URLConnection connection = url.openConnection();

    HttpURLConnection httpConn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

byte[] buffer = new byte[sbr.toString().length()];
    System.out.println(sbr.toString());
    buffer = sbr.toString().getBytes();
    bout.write(buffer);
    byte[] b = bout.toByteArray();
    String SOAPAction = "http://litwinconsulting.com/webservices/GetWeather";
    // Set the appropriate HTTP parameters.
//  httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
    //httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpConn.setRequestProperty("SOAPAction", SOAPAction);
    httpConn.setRequestMethod("POST");
    //httpConn.setChunkedStreamingMode(-1);
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setRequestMethod("POST");


    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();
    //       Send SOAP Message to SOAP Server
    SOAPMessage message = createSOAPRequest(sbr.toString());
    message.getMimeHeaders().setHeader("Content-Type", "application/xop+xml; type=\"text/xml\"; charset=utf-8");

    AttachmentPart attachment = message.createAttachmentPart();
    attachment.setContent(new File("C://Desktop//test.txt"), "text/plain");

    attachment.setContentId("c0ec843c-a975-4abf-bd12-4b4cc46ea6c0");
    message.addAttachmentPart(attachment);

     message.saveChanges();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        message.writeTo(baos);
        String msg = baos.toString();
        System.out.println(msg);

        SOAPMessage soapResponse = soapConnection.call(message, wsURL);

        String response = getResponseString(soapResponse);
        System.out.println(response);

我收到以下错误

  

com.sun.xml.internal.messaging.saaj.soap.MessageImpl初始化严重:   SAAJ0535:无法内部化消息

0 个答案:

没有答案