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 を送信しても、サーバーがログイン ページに戻ることです。誰かが私を助けることができますか?