HttpUrlConnectionダウンロードクッキーとアクセス時にクッキー付き予約が必要

1563 ワード

転載先:

HttpURLConnectionシミュレーションユーザーログイン


これも転載ですが、この点は私もテストできるサイトを見つけてからテストを行って確認してから、私のツール類に記入する必要があります.
URL url = new URL(" ");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);//  
connection.setRequestMethod("POST");//  “GET”、“POST”
String content = "username=admin&password=admin";
connection.setRequestProperty("Cookie", responseCookie);//  session id 
OutputStream os = connection.getOutputStream();
os.write(content.toString().getBytes("GBK"));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseCookie = connection.getHeaderField("Set-Cookie");//  Cookie
String sessionIdString = "";
if (responseCookie != null) {
    sessionIdString = responseCookie.substring(0, responseCookie.indexOf(";"));
}
//  
String line = br.readLine();
while (line != null) {
    System.out.println(line);
    line = br.readLine();
}
// access
URL url1 = new URL(" ");
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
connection1.setRequestProperty("Cookie", responseCookie);//  cookie
BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));
String line1 = br1.readLine();
while (line1 != null) {
    // TODO: 
}