小编典典

Java轴Web服务客户端setMaintainSession在多个服务上(Cookie?)

java

我正在实现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!

那我该怎么做才能使这项工作呢?


阅读 270

收藏
2020-11-26

共1个答案

小编典典

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会公开更多可以设置的方法和属性。很高兴知道以后我猜:-)

2020-11-26