【问题标题】:Java CXF WS Client - gZip HTTP REQUEST HEADER being ignoredJava CXF WS 客户端 - gZip HTTP REQUEST HEADER 被忽略
【发布时间】:2013-01-28 04:10:05
【问题描述】:

我试图在我的 Java CXF WS 客户端内的 HTTP 请求标头中设置/请求 gZip,但由于某种原因它被忽略了。我没有得到 gZipped 响应。这是我尝试设置的方式。我正在使用 Apache CXF 2.3.2。我错过了什么?

public class LoggerXML implements SOAPHandler<SOAPMessageContext> {
    private String uniqueIdentifier;
    private String sessionId;

    public LoggerXML(String sessionId, String uniqueIdentifier) {
        this.sessionId = sessionId;
        this.uniqueIdentifier = uniqueIdentifier;
    }

    protected final void setLogStream(PrintStream ps) {
      //  out = ps;
    }

    public void init(Map c) {
        uniqueIdentifier = "";
    }

    public Set<QName> getHeaders() {
        return null;
    }

    public boolean handleMessage(SOAPMessageContext smc) {

        Boolean outboundProperty = (Boolean)
        smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if(outboundProperty){
            // Creating HTTP headers & setting gZip.
            Map<String, List<String>> headers = (Map<String, 
                                        List<String>>) smc.get(MessageContext.HTTP_REQUEST_HEADERS);
            if(headers == null){
                //System.out.println("LoggerXML.handleMessage: headers = null");
                headers = new HashMap<String, List<String>>();  
            }
            // Add HTTP headers to the web service request
            headers.put("Accept-Encoding", Collections.singletonList("gzip,deflate"));
            //headers.put("Content-Encoding", Collections.singletonList("gzip"));
            //headers.put("Accept-Encoding", Collections.singletonList("gzip"));

            smc.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
            //smc.put("org.apache.cxf.transport.common.gzip.GZIPOutInterceptor.UseGzip","YES");

        }

        return true;
    }

    public boolean handleFault(SOAPMessageContext smc) {
        return true;
    }

    // nothing to clean up
    public void close(MessageContext messageContext) {
    }

    // nothing to clean up
    public void destroy() {
    }

// Other Methods....

}

【问题讨论】:

    标签: java http-headers cxf


    【解决方案1】:

    这段代码对我有用

        // Get the underlying Client object from the proxy object of service interface
    Client proxy = ClientProxy.getClient(stub);
    
       // Creating HTTP headers
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Accept-Encoding", Arrays.asList("gzip"));
    
        // Add HTTP headers to the web service request
    proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
    

    参考:http://singztechmusings.wordpress.com/2011/09/17/apache-cxf-how-to-add-custom-http-headers-to-a-web-service-request/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多