Setting a custom HTTP header dynamically with Spring-WS client

后端 未结 7 590
我在风中等你
我在风中等你 2020-12-24 05:53

How do you set a custom HTTP header (not SOAP header) dynamically on the client side when using Spring-WS?

7条回答
  •  别那么骄傲
    2020-12-24 06:39

    ClientInterceptor works great for static header value. But it is not possible to use it when a different value should be applied per each request. In that case WebServiceMessageCallback is helpful:

    final String dynamicParameter = //...
    
    webServiceOperations.marshalSendAndReceive(request, 
        new WebServiceMessageCallback() {
            void doWithMessage(WebServiceMessage message) {
                TransportContext context = TransportContextHolder.getTransportContext();
                CommonsHttpConnection connection = (CommonsHttpConnection) context.getConnection();
                PostMethod postMethod = connection.getPostMethod();
                postMethod.addRequestHeader( "fsreqid", dynamicParameter );
            }
    }
    

提交回复
热议问题