java轴web服务客户端setMaintainSession多个服务(Cookie?) [英] java axis web service client setMaintainSession on multiple services (cookies?)

查看:153
本文介绍了java轴web服务客户端setMaintainSession多个服务(Cookie?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个客户端到Web服务(和维护Web服务的家伙一直没有反应..)我使用axis和WSDL2Java来生成java类,我可以调用他们的登录方法认证服务确定,并得到一个sessionId(例如z4zojhiqkw40lj55kgtn1oya)。但是,似乎我不能使用这个sessionId作为参数在任何地方。即使调用他们的hasSession() - 方法在登录后直接返回false。我设法通过在这个服务的Locator对象上设置setMaintainSession(true)来解决这个问题。但问题是,第一个服务,认证服务,只用于认证。如果我然后调用setMaintainSession(true)例如ProductServiceLocator,并调用其上的一些方法,我会得到一个错误,因为未经身份验证的会话。我必须找到一种方法来在客户端的服务之间共享会话。
查看他们的php代码示例 - 它看起来像他们将会话存储在cookie中。我如何在我的java客户端模仿这种行为?
php-code:

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

我的代码如下:

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?

推荐答案

Hooray,我终于解决了自己:-D
Thanx很多的优秀文章 http://www.nsftools.com/stubby/ApacheAxisClientTips.htm
我必须做以下与我的代码,使其工作:

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

在自动生成类的高级抽象中操作,对我来说,将服务类转换为Stub会暴露更多可以设置的方法和属性。很好知道以后我猜猜: - )

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 :-)

这篇关于java轴web服务客户端setMaintainSession多个服务(Cookie?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆