如何在java中的soaprequest中添加http标头

tgr*_*tgr 8 java soap axis2 header

我尝试连接到Yahoo webservice.我按轴2生成了类.我现在面临的问题是,web服务需要标题中的特定键值对,我绝对不能这样做.我在网上搜索并找到了不同的可能性 - 它们都不适合我.最有希望的是几乎在本页末尾的帖子,Claude Coulombe为了更改生成的存根的代码而消化,但这也失败了.有谁能告诉我如何解决这个问题?

编辑

使用Options的建议方式产生以下异常:

Exception in thread "main" org.apache.axis2.AxisFault: Address information does not exist in the Endpoint Reference (EPR).The system cannot infer the transport mechanism.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

val stub = new IndexToolsApiServiceStub("https://api.web.analytics.yahoo.com/IndexTools/services/IndexToolsApiV3")

val client = stub._getServiceClient
val options = new Options
val list = new ArrayList[Header]()
val header = new Header
header.setName("YWA_API_TOKEN")
header.setValue("NOTtheREALvalue")
list.add(header)
options.setProperty(HTTPConstants.HTTP_HEADERS, list)
client.setOptions(options)
stub._setServiceClient(client)
Run Code Online (Sandbox Code Playgroud)

dav*_*son 5

您可能要使用Axis2选项

// Create an instance of org.apache.axis2.client.ServiceClient
ServiceClient client = ...

// Create an instance of org.apache.axis2.client.Options
Options options = new Options();

List list = new ArrayList();

// Create an instance of org.apache.commons.httpclient.Header
Header header = new Header();

// Http header. Name : user, Value : admin
header.setName("user");
header.setValue("admin");

list.add(header);
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, list);

client.setOptions(options);
Run Code Online (Sandbox Code Playgroud)

这是该代码的参考


tgr*_*tgr 0

两个月前我找到了解决这个问题的方法。您无法使用 Axis2 设置自定义标头。所以我回到了旧的 Axis 版本,在那里你可以做到这一点。自己设置 Http 标头并不是一个好的做法,而且大多是不必要的。最重要的是,它不是 SOAP 规范的一部分。这就是为什么你不能用 Axis2 做到这一点的原因。