如何在java中的webservice调用中添加或操作http头

时间:2015-04-19 09:29:49

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

需要在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>

如何在发出soap请求时在http标头中添加新的键值对(New-Key:Some dynamic value)?稍后,服务器将从http标头中提取值。作为要求,新的领域不应该添加到肥皂头中,也不应添加到肥皂体中。

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

1 个答案:

答案 0 :(得分:0)

Ans at
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”);
}

}