[英]java axis web service client setMaintainSession on multiple services (cookies?)
我正在實現Web服務的客戶端(並且維護Web服務的家伙反應遲鈍。。)我使用axis和WSDL2Java生成Java類,並且可以在身份驗證服務上調用其登錄方法好的,然后獲取sessionId(例如z4zojhiqkw40lj55kgtn1oya)。 但是,似乎我無法在任何地方將此sessionId用作參數。 甚至在登錄后直接調用其hasSession()方法也會返回false。 我設法通過在此服務的Locator-object上設置setMaintainSession(true)來解決此問題。 但是問題在於,第一個服務Authentication-service僅用於身份驗證。 如果隨后在例如ProductServiceLocator上調用setMaintainSession(true),並對其調用某些方法,則由於未認證的會話,我將收到錯誤消息。 我必須找到一種在客戶端的服務之間共享會話的方法。 查看他們的php代碼示例-好像他們將會話存儲在cookie中。 如何在Java客戶端中模仿此行為? PHP代碼:
$authentication = new SoapClient ( "https://webservices.24sevenoffice.com/authenticate/authenticate.asmx?wsdl", $options );
// log into 24SevenOffice if we don't have any active session. No point doing this more than once.
$login = true;
if (!empty($_SESSION['ASP.NET_SessionId'])){
$authentication->__setCookie("ASP.NET_SessionId", $_SESSION['ASP.NET_SessionId']);
try{
$login = !($authentication->HasSession()->HasSessionResult);
}
catch ( SoapFault $fault ) {
$login = true;
}
}
if( $login ){
$result = ($temp = $authentication->Login($params));
// set the session id for next time we call this page
$_SESSION['ASP.NET_SessionId'] = $result->LoginResult;
// each seperate webservice need the cookie set
$authentication->__setCookie("ASP.NET_SessionId", $_SESSION['ASP.NET_SessionId']);
// throw an error if the login is unsuccessful
if($authentication->HasSession()->HasSessionResult == false)
throw new SoapFault("0", "Invalid credential information.");
}
我的代碼如下:
AuthenticateLocator al = new AuthenticateLocator();
al.setMaintainSession(true);
Credential c = new Credential(CredentialType.Community,username,password,guid);
AuthenticateSoap s = al.getAuthenticateSoap();
String sessionId = s.login(c);
System.out.println("Session id was: "+sessionId);
System.out.println("Has Session: "+s.hasSession()); //Hooray, now works after setMaintainSession(true)
//And now trying to call another Service
CompanyServiceLocator cl = new CompanyServiceLocator();
cl.setMaintainSession(true);
CompanyServiceSoap css = cl.getCompanyServiceSoap();
css.getCountryList(); //FAILS!
那么我該怎么做才能使這項工作呢?
Hooray,我終於自己解決了它:-D非常感謝http://www.nsftools.com/stubby/ApacheAxisClientTips.htm上的出色文章,我必須對我的代碼進行以下操作才能使其正常工作:
CompanyServiceLocator cl = new CompanyServiceLocator();
cl.setMaintainSession(true);
CompanyServiceSoap css = cl.getCompanyServiceSoap();
((Stub)css)._setProperty(HTTPConstants.HEADER_COOKIE, "ASP.NET_SessionId="+sessionId); //New line that does the magic
css.getCountryList(); //SUCCESS :-D
以自動生成類的高級抽象進行操作時,我不知道將服務類強制轉換為Stub會公開更多可以設置的方法和屬性。 很高興知道以后我猜:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.