JAX-WS客户端获取响应头

时间:2015-09-29 21:44:26

标签: java web-services jax-ws

我正在尝试实施一些逻辑。使用JAX-WS客户端(wsimport)调用WCF服务后,出现HTTP 401403错误。

  

com.sun.xml.ws.client.ClientTransportException:服务器发送了HTTP   状态码401:未经授权

如何在拨打电话后获得响应标头?我需要实施SOAPHandler.class吗?

2 个答案:

答案 0 :(得分:1)

可以在传入响应的客户端访问HTTP标头。以下是Handler代码,演示了如何执行此操作(请Access HTTP headers in a Handler查看GlassFish Metro):

public class ClientHandler implements SOAPHandler<SOAPMessageContext> {
    public boolean handleMessage(SOAPMessageContext context) {
        if (!(Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
            Map<String, List<String>> map = (Map<String, List<String>>) 
                context.get(MessageContext.HTTP_RESPONSE_HEADERS);
            List<String> contentType = getHTTPHeader(map, "Content-Type");
            if(contentType != null){
                StringBuffer strBuf = new StringBuffer();
                for(String type : contentType){
                    strBuf.append(type);
                }
                System.out.println("Content-Type:"+strBuf.toString());
            }
        }
        return true;
    }


    private @Nullable
    List<String> getHTTPHeader(Map<String, List<String>> headers, String header){
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String name = entry.getKey();
            if(name.equalsIgnoreCase(header))
                return entry.getValue();
        }
        return null;
    }
}

答案 1 :(得分:0)

我找到了解决方案:

(Integer) ((BindingProvider) servClient).getResponseContext().get(MessageContext.HTTP_RESPONSE_CODE);