簡體   English   中英

如何在java中向soaprequest添加http頭

[英]how to add a http header to a soaprequest in java

我嘗試連接到雅虎網絡服務。 我通過axis2生成了類。 我現在面臨的問題是,Web 服務需要標頭中的特定鍵值對,而我絕對不能這樣做。 我在網上搜索並發現了不同的可能性——它們都不適合我。 最有希望的是幾乎在本頁末尾的帖子,Claude Coulombe 建議更改生成的存根的代碼,但這也失敗了。 有人可以告訴我如何解決這個問題嗎?

編輯

使用選項的建議方法產生了以下異常:

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選項

// 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);

這是該代碼的參考

如果要將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);`

兩個月前我找到了解決這個問題的方法。 您無法使用Axis2設置自定義標頭。 所以我回到了舊的Axisversion,你可以做到這一點。 自己設置Http-header並不是一個好習慣,而且大多是不必要的。 最重要的是,它不是SOAP規范的一部分。 這就是為什么你不能用Axis2做到這一點的原因。

實際上,您只需從ServiceClient檢索選項引用,而不是替換選項對象。 然后添加所需的屬性:

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

我也有同樣的問題,解決方案是Barbiturica:沒有添加標題選項

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

這個頁面是missleading: reference

解決方案 -

    MyStub stub = new MyStub();
    ServiceClient serviceClient = stub._getServiceClient();
    Options options = serviceClient.getOptions();
    List<NamedValue> namedValuePairs = new ArrayList<NamedValue>();
    namedValuePairs.add(new NamedValue("Authorization", "Basic JSDFANSKMSLAWQEINCCAKNASKNAS2371BASCKA="));
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, namedValuePairs);

只需要在選項上設置一個額外的屬性,不要創建一個新的選項變量,如下所示,這對我來說沒有幫助。

Options options = new Options();

此外,直接設置HTTPConstants.AUTHENTICATE不起作用(如下設置)

clientOptions.setProperty(HTTPConstants.AUTHENTICATE, auth);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM