java 如何在java中向soaprequest添加http头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12071798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:28:47  来源:igfitidea点击:

how to add a http header to a soaprequest in java

javasoapheaderaxis2

提问by tgr

I try to connect to a Yahoo webservice. I generated the classes by axis2. The problem I am facing right now is, that the webservice requires a specific key value pair in the header and I am absolutely not able, to do so. I searched the web and found different possibilities - none of them worked for me. The most promissing was the post nearly at the end of this page, were Claude Coulombe sugested to change the code of the generated stub, but this failed as well. Can anybody show me a way how to solve this issue?

我尝试连接到雅虎网络服务。我通过axis2生成了类。我现在面临的问题是,Web 服务需要标头中的特定键值对,而我绝对不能这样做。我在网上搜索并发现了不同的可能性——它们都不适合我。最有希望的是几乎在本页末尾的帖子,Claude Coulombe 建议更改生成的存根的代码,但这也失败了。有人可以告诉我如何解决这个问题吗?

Edit

编辑

The suggested way using Options produced the following exception:

使用选项的建议方法产生了以下异常:

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.

Here is my code:

这是我的代码:

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)

采纳答案by tgr

I found a solution for the problem two months ago. You are not able to set a customized header with Axis2. So I fell back to the older Axisversion, where you can do it. Setting the Http-header by yourself is no good practice and mostly unneccesary. On top it is not a part of the SOAP-specification. That is the reason why you cannot do it with Axis2.

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

回答by davidfmatheson

You probably want to use Axis2's Options:

您可能想要使用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);

Here's the referencefor that code.

这是该代码的参考

回答by Majid

It doesn't matter if you want to add HTTP headers to your SOAP request or response. either ways you should work with MessageContext. Supposed that msgContext is your Axis2 request/response Message Context object (org.apache.axis2.context.MessageContext), below code will do the trick and using it, you can add HTTP headers.

是否要将 HTTP 标头添加到 SOAP 请求或响应中并不重要。无论哪种方式,您都应该使用 MessageContext。假设 msgContext 是您的 Axis2 请求/响应消息上下文对象 (org.apache.axis2.context.MessageContext),下面的代码将完成并使用它,您可以添加 HTTP 标头。

`//Instantiate an Options object from org.apache.axis2.client.Options
 Options options = new Options();
 //Instantiate an ArrayList of type NamedValue from org.apache.axis2.context.NamedValue
 List<NamedValue> namedValuePairs = new ArrayList<NamedValue>();
 //Add as much as headers you want using below code
 namedValuePairs.add(new NamedValue("sample", "value"));
 //Finally add namedValuePairs to options, and add options to msgContext
 options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, namedValuePairs);
 msgContext.setOptions(options);`

回答by Barbiturica

Actually, you just have to retrieve the options reference from the ServiceClientinstead of replacing the options object. Then add the properties you want:

实际上,您只需要从 中检索选项引用ServiceClient而不是替换选项对象。然后添加你想要的属性:

ServiceClient sc = awst._getServiceClient();
Options ops = sc.getOptions();

回答by user2148242

I too had the same problem the solution is that of Barbiturica: add header option without

我也有同样的问题,解决方案是 Barbiturica: add header option without

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

this page is missleading: reference

此页面具有误导性:参考