【问题标题】:how to get session id on client side in apache httpclient如何在apache httpclient中获取客户端的会话ID
【发布时间】:2012-09-17 23:34:09
【问题描述】:

我正在验证尝试使用用户名和密码登录到 Web 服务。 然后我必须在随后的我们调用中使用 sessionid 。我正在使用 apache HttpClient (legacy version) 2.0.2 .

下面是我的客户端验证代码。我的问题是,我如何在身份验证后获取会话 ID 以及如何在后续调用中使用相同的会话 ID。

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class TestHttpClient {

    public static void main(String args[]) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new HttpGet("www.google.com");

            org.apache.commons.httpclient.UsernamePasswordCredentials upc =
                    new org.apache.commons.httpclient.UsernamePasswordCredentials("user", "pwd");

            client.getState().setCredentials(null, null, upc);

            get.setDoAuthentication(true);

            client.setConnectionTimeout(60000);

            client.executeMethod(get);

            System.out.println(get.getResponseBodyAsString());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

感谢您帮助新手进行网络开发。

【问题讨论】:

    标签: java web-services httpclient session-variables apache-httpclient-4.x


    【解决方案1】:

    以下是获取 Session id 的方法:

    .......
    
    client.executeMethod(get); 
    
    Header[] headers=get.getResponseHeader("jsessionid");
    
    if(headers!=null && headers.length>0){
    
     String sessionId=headers[0].getValue();
    
    }
    

    我相信会有更好的方法来处理经过身份验证的会话,而无需您读取会话 ID 往复。

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 2014-07-24
      • 2015-03-16
      • 2022-01-21
      • 1970-01-01
      • 2013-09-09
      • 2018-03-25
      • 2014-02-08
      • 2020-11-10
      相关资源
      最近更新 更多