登录到应用程序后,我正在重定向到登录页面。
代码:
public class Code {
public static void main(String args[]) {
String stringResponse = null;
String LOGIN_URL_INTIAL = "http://localhost:15280/****/login";
String BASE_URL = "http://localhost:15280";
String LOGIN_URL = BASE_URL + "/api/dologin";
String USERNAME = "****";
String PASSWORD = "*****";
String cookieString = "";
String cookie = null;
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
//CloseableHttpClient httpclient = HttpClients.createDefault();
// CloseableHttpClient httpclient1 = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet(LOGIN_URL_INTIAL);
CloseableHttpResponse response1 = httpclient.execute(httpget);
try {
HttpEntity entity = response1.getEntity();
System.out.println("Login form get: " + response1.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None >>>>>>");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Intially---> " + cookies.get(i).toString());
}
}
} finally {
response1.close();
}
HttpUriRequest login = RequestBuilder.post().setUri(new URI(LOGIN_URL)).addParameter("j_username", USERNAME)
.addParameter("j_password", PASSWORD).build();
CloseableHttpResponse response2 = httpclient.execute(login);
try {
HttpEntity entity = response2.getEntity();
System.out.println("Login form get: " + response2.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
System.out.println("- " + cookies.get(i).toString().split("value:")[1].split("]")[0]);
cookieString = cookies.get(i).toString().split("value:")[1].split("]")[0];
System.out.println("Cookies:" + cookieString + "\n");
cookie = cookieString;
}
}
HttpUriRequest requestDashborad = RequestBuilder.get()
.setUri("http://localhost:15280/****/secure/dashboard").setHeader("Set-Cookie", cookie)
.setHeader("Accept", "*/*").setHeader("Accept-Encoding:", "gzip, deflate, br")
.setHeader("Accept-Language", "en-US,en;q=0.8")
.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3153.0 Safari/537.36")
.build();
CloseableHttpResponse requestDashboradResponse = httpclient.execute(requestDashborad);
HttpEntity entity1 = requestDashboradResponse.getEntity();
// Read the contents of an entity and return it as a String.
String content = EntityUtils.toString(entity1);
stringResponse = content;
String q = "SELECT * FROM ds_movieSearch&maxRows=5";
HttpUriRequest request = RequestBuilder.get()
.setUri("http://localhost:15280/****/soap/v1/****/select?query="
+ URLEncoder.encode(q, "UTF-8"))
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json").build();
CloseableHttpResponse response3 = httpclient.execute(request);
System.out.println("dasd--->" + response3);
// stringResponse= response3;
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
}
登录应用程序后。我的状态为302.之后,我尝试访问仪表板页面,将其重定向到登录页面。
我认为cookie会被重置为默认值。我可以知道如何维护httpCclient会话吗?
答案 0 :(得分:0)
状态302为SC_MOVED_TEMPORARILY
。这意味着服务器将客户端重定向到新URL。如果您没有获得该新URL,则很可能登录过程不会在服务器端完成,并且服务器可能会在下次请求时创建一个新的(仍然没有进入)会话。
您必须关注具有302状态的响应的位置标头。