我尝试连接到 Yahoo 网络服务。我通过 axis2 生成了类。我现在面临的问题是,Web 服务需要 header 中的特定键值对,而我绝对不能这样做。我在网上搜索并发现了不同的可能性——它们都不适合我。最有前途的是接近 this page 末尾的帖子, Claude Coulombe 是否建议更改生成的 stub 的代码,但这也失败了。谁能告诉我如何解决这个问题?
编辑
建议的使用选项的方式产生了以下异常:
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.
这是我的代码:
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)
最佳答案
您可能想使用 Axis2
的 Options :
// 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);
这是 reference对于该代码。
关于java - 如何在 java 中将 http header 添加到 soaprequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071798/