【问题标题】:How to add or manipulate http header in a webservice call in java如何在 java 中的 web 服务调用中添加或操作 http 标头
【发布时间】:2015-06-26 00:14:27
【问题描述】:

需要在soap 请求中添加新的http 标头键值对。如下所示。

 HTTP/1.1 200 OK
 Cache-Control: no-cache
 Pragma: no-cache
 Content-Type: text/xml; charset=utf-8
 New-Key: Some dynamic value

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Body>

如何在发出肥皂请求时在 http 标头中添加新的键值对(新键:某些动态值)?稍后将由服务器从 http 标头中提取值。作为要求,新字段不应添加到肥皂标题或肥皂正文中。

我们正在使用 IBM wsdl2java 工具,它实现了 IBM JAX-RPC 规范来生成 Java 客户端并调用 SOAP Web 服务调用。

【问题讨论】:

    标签: web-services soap http-headers jax-rpc custom-headers


    【解决方案1】:

    回答
    https://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.base.doc/ae/rwbs_transportheaderproperty.html

    public class MyApplicationClass {
    // Inject an instance of the service's port-type.
    @WebServiceRef(EchoService.class)
    private EchoPortType port;
    
    // This method will invoke the web service operation and send and receive transport headers.
    public void invokeService() {
    
        // Set up the Map that will contain the request headers.
        Map<String, Object>requestHeaders = new HashMap<String, Object>();
        requestHeaders.put(“Cookie”, “ClientAuthenticationToken=FFEEBBCC”);
        requestHeaders.put(“MyHeaderFlag”, new Boolean(true));
    
        // Set the Map as a property on the RequestContext.
        BindingProvider bp = (BindingProvider) port;
        bp.getRequestContext().put(com.ibm.websphere.webservices.Constants.REQUEST_TRANSPORT_PROPERTIES, requestHeaders);
    
        // Set up the Map to retrieve transport headers from the response message.
        Map<String, Object>responseHeaders = new HashMap<String, Object>();
        responseHeaders.put(“Set-Cookie”, null);
        responseHeaders.put(“MyHeaderFlag, null);
    
        // Invoke the web services operation.
        String result = port.echoString(“Hello, world!”);
    
        // Retrieve the headers from the response.
        String cookieValue = responseHeaders.get(“Set-Cookie”);
        String headerFlag = responseHeaders.get(“MyHeaderFlag”);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2018-07-08
      • 1970-01-01
      • 2010-10-28
      • 2021-08-21
      • 2021-09-06
      相关资源
      最近更新 更多