gpt4 book ai didi

java - 如何在apache httpclient中获取客户端的 session ID

转载 作者:行者123 更新时间:2023-12-01 15:09:59 25 4
gpt4 key购买 nike

我正在尝试使用用户名和密码登录网络服务进行身份验证。然后我必须在后续调用中使用 sessionid 。我正在使用 apache HttpClient (旧版本)2.0.2 。

下面是我的客户端代码来进行身份验证。我的问题是,在身份验证后如何获取 session ID 以及如何在后续调用中使用相同的 session 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();
}
}

}

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

最佳答案

以下是获取 session ID 的方法:

.......

client.executeMethod(get);

Header[] headers=get.getResponseHeader("jsessionid");

if(headers!=null && headers.length>0){

String sessionId=headers[0].getValue();

}

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

关于java - 如何在apache httpclient中获取客户端的 session ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12468506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com