Java 如何在ApacheHttpClient中获取客户端会话id
我正在验证是否尝试使用用户名和pwd登录到Web服务。 然后我必须在后续的we调用中使用sessionid。我使用的是ApacheHttpClient(旧版)2.0.2 下面是我要验证的客户端代码。我的问题是,在我进行身份验证之后如何获取会话Id,以及如何在后续调用中使用相同的会话IdJava 如何在ApacheHttpClient中获取客户端会话id,java,web-services,httpclient,session-variables,apache-httpclient-4.x,Java,Web Services,Httpclient,Session Variables,Apache Httpclient 4.x,我正在验证是否尝试使用用户名和pwd登录到Web服务。 然后我必须在后续的we调用中使用sessionid。我使用的是ApacheHttpClient(旧版)2.0.2 下面是我要验证的客户端代码。我的问题是,在我进行身份验证之后如何获取会话Id,以及如何在后续调用中使用相同的会话Id import java.io.IOException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpcl
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();
}
}
}
感谢您在web开发方面对新手的帮助。以下是获取会话id的方法:
.......
client.executeMethod(get);
Header[] headers=get.getResponseHeader("jsessionid");
if(headers!=null && headers.length>0){
String sessionId=headers[0].getValue();
}
我相信有更好的方法来处理经过身份验证的会话,而无需读取会话id