我正在开发一个小程序,在基于会话的屏幕上通过HttpUrlConnection
执行登录,保存cookie并使用保存的cookie转到另一个页面。我的代码如下所示:
public class LoginConnection {
private final String USER_AGENT = "Mozilla/5.0";
private CookieStore cookieJar;
public static void main(String[] args) throws Exception {
LoginConnection http = new LoginConnection();
http.sendPost();
http.sendGet();
}
public LoginConnection() {
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
cookieJar = manager.getCookieStore();
}
private void sendPost() throws Exception {
String params = "login=username&password=passwd";
URL url = new URL("http://www.domain.com/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
// Send post request
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
// send request
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(params);
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, response = "";
while ((line = in.readLine()) != null) {
response += line + "\n";
}
in.close();
String headerName = null;
for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = connection.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
HttpCookie httpCookie = new HttpCookie(cookieName, cookieValue);
httpCookie.setDomain("http://www.domain.com/");
httpCookie.setPath("/");
httpCookie.setVersion(0);
cookieJar.add(new URI("http://www.domain.com/"), httpCookie);
}
}
// show cookies (sessionid)
System.out.println("CookieHandler retrieved cookie: " + cookieJar.getCookies().get(0).toString());
// print result
System.out.println(response.toString().replaceAll("\\s+", " "));
}
// HTTP GET request
private void sendGet() throws Exception {
URL url = new URL("http://www.domain.com/profile");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Cookie", cookieJar.getCookies().get(0).toString());
con.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line, response = "";
while ((line = in.readLine()) != null) {
response += line + "\n";
}
in.close();
// show cookies (sessionid)
System.out.println("CookieHandler retrieved cookie: " + cookieJar.getCookies().get(0).toString());
// print result
System.out.println(response.toString().replaceAll("\\s+", " "));
}
}
问题是即使发送cookie,服务器也会返回登录页面。有人能帮助我吗?