首页 > 解决方案 > apache httpclient - 如何反序列化响应

问题描述

我正在通过 HttpClient 调用外部 Web 服务,但在转换我得到的响应时遇到了问题。这是一个像这样的mtom响应:

--MIMEBoundaryurn_uuid_5CF5D04EE84169F28B1567408992661 内容类型:application/xop+xml;字符集=UTF-8;type="text/xml" Content-Transfer-Encoding: 二进制 Content-ID: <0.urn:uuid:5CF5D04EE84169F28B1567408992662@apache.org>

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns3:executeResponse xmlns:ns2="http://www.myservice.com/bside/plugins/webservices/bigdata/types" xmlns:ns3="http://www.myservice.com/bside/plugins/webservices/bigdata/ws/THCommand/">
            <returnCode>WS000000</returnCode>
            <returnCodeDescription>Processing completed successfully.</returnCodeDescription>
            <systemInfo>
                <ns2:userId>111000</ns2:userId>     <ns2:rsId>619344</ns2:rsId>
                <ns2:sessionId>550211</ns2:sessionId>
            </systemInfo>
            <responseFile>
                <ns2:name>052015_619344_XXXX_YYYY_1567408989171.zip.crypt</ns2:name>
                <ns2:content>
                    <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:5CF5D04EE84169F28B1567408992663@apache.org"/>
                </ns2:content>
            </responseFile>
        </ns3:executeResponse>
    </soapenv:Body>
</soapenv:Envelope>

--MIMEBoundaryurn_uuid_5CF5D04EE84169F28B1567408992661 内容类型:应用程序/八位字节流内容传输编码:二进制内容ID:

���-��[�-O� �g/Ü?��s�7��82��!x�ګEï¿½ï ¿½|^��.c׋���ء�~��!�CI6�C_��]o��ux�y�lï ¿½ï¿½ï¿½X���Ӯ����C�i��7c���<��*�"-/� ,x��l�/0�J�(�d?�=�>��BXP���O��}ï¿½ï¿½ï¿½ï ¿½`���T��;

我试图在这里和那里遵循建议,我想出了这个测试类https://svn.apache.org/repos/asf/cxf/branches/2.7.x-fixes/api/src/test/java/org/ apache/cxf/attachment/AttachmentDeserializerTest.java

但不幸的是它不起作用或更好,我忘记做一些很容易的事情。

这是我为它编写的一些代码:

HttpResponse 响应 = client.execute(httpPost); HttpEntity 实体 = response.getEntity();

    String ct = response.getLastHeader("Content-Type").toString().replace("Content-Type: ", "");

    String responseContent = null;

    if (entity != null) {
        responseContent = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());

        MessageImpl msg = new MessageImpl();
        Exchange exchange = new ExchangeImpl();
        msg.setExchange(exchange);
        InputStream is = new ByteArrayInputStream(responseContent.getBytes());
        msg.put(Message.CONTENT_TYPE, ct);
        msg.setContent(InputStream.class, is);
        AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
        deserializer.initializeAttachments();
        InputStream attBody = msg.getContent(InputStream.class);

        String attMimeType = msg.getAttachmentMimeType();

        Collection<Attachment> atts = msg.getAttachments();
        Iterator<Attachment> itr = atts.iterator();
        Attachment a = itr.next();
        InputStream attIs = a.getDataHandler().getInputStream();

    }

不幸的是,集合中没有元素......我的目标是处理元素,但我陷入了这种转换。预先感谢您的支持。

标签: javaapacheapache-httpclient-4.x

解决方案


推荐阅读