如何在Java中的Web服务调用中添加或操作HTTP标头 [英] How to add or manipulate http header in a webservice call in java

查看:253
本文介绍了如何在Java中的Web服务调用中添加或操作HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在soap请求中添加新的http标头键值对.像下面这样.

There is a requirement to add a new http header key value pair in a soap request. Something like below.

 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标头中添加新的键值对(New-Key:某些动态值)?服务器稍后将从http标头中提取值.作为一项要求,不应在肥皂标头或肥皂主体中添加新字段.

How can I add a new key value pair (New-Key: Some dynamic value) in http header while making a soap request? Value will later get extracted by server from http header. As a requirement new field should neither be added in soap header nor in soap body.

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

We are using IBM wsdl2java tool, which implements the IBM JAX-RPC specification to generate the Java clients and invoke the SOAP web service calls.

推荐答案

答案在
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");
}

}

这篇关于如何在Java中的Web服务调用中添加或操作HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆