2012-08-22 31 views
6

我嘗試連接到Yahoo webservice。我通過axis2生成類。我現在面臨的問題是,web服務需要在標題中使用特定的鍵值對,我絕對無法這樣做。我搜索了網絡,發現了不同的可能性 - 它們都不適合我。最有希望的是幾乎在this page末尾的帖子,Claude Coulombe被要求改變生成的存根的代碼,但是這也失敗了。任何人都可以告訴我一個方法如何解決這個問題?如何將http頭添加到java中的soaprequest中

編輯

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

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) 
+0

我假設你的問題是關於HTTP頭(不是HTML)。您可能要更正錯字... –

+0

不好意思,你說得對。 –

回答

0

兩個月前我找到了解決方案。您無法使用Axis2設置自定義標題。所以我回到了舊的Axisversion,在那裏你可以做到這一點。自行設置Http標頭並不是一種好的做法,而且大多都是不必要的。最重要的是它不是SOAP規範的一部分。這就是爲什麼你不能用Axis2來做這件事的原因。

4

你可能想使用Axis2Options

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

0

實際上,您只需從ServiceClient中檢索選項參考,而不是替換選項對象。然後添加你想要的屬性:

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

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

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

此頁面missleading:reference

1

它如果要將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);` 
相關問題