java axis web service client setMaintainSession on multiple services (cookies?)

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I'm implementing a client to a web service (and the guys maintaining the web service have been a litte unresponsive..) I've used axis and WSDL2Java to generate java classes and I can call their login-method on their authentication-service ok, and get a sessionId back (eg z4zojhiqkw40lj55kgtn1oya). However, it seems that i cannot use this sessionId as a parameter anywhere. Even a call to their hasSession()-method directly after login returned false. I managed to solve this by setting setMaintainSession(true) on the Locator-object for this service. But the problem is, that this first service, the Authentication-service, is only used for authentification. If I then call setMaintainSession(true) on eg ProductServiceLocator, and call some method on it, I will get an error because of unauthenticated session. I have to find a way to share the session between the services on the client side. Looking on their php code example-it seeems like they are storing the session in a cookie. How can I mimic this behaviour in my java client? php-code:

$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."); } 

My code is the following:

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! 

So what can I do to make this work?

回答1:

Hooray, I finally solved it myself :-D Thanx a lot to the excellent article at http://www.nsftools.com/stubby/ApacheAxisClientTips.htm I had to do the following with my code to make it work:

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 

Operating in the high-level abstraction of the autogenerated classes, it was unknown to me that casting the service classes to Stub would expose more methods and properties that could be set. Good to know for later I guess :-)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!