쿠키를 가져오면 다음 요청에 쿠키를 둡니다.

6182 단어 코드cookie
쿠키를 가져온 후 쿠키를 다음 요청 코드에 두면 다음과 같습니다.
import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class JsoupTest {

    public static void main(String[] args) throws IOException {
        goHttp();
    }

    public static  final String returnCookies() {

        try {
            Connection conn = Jsoup.connect("https://xueqiu.com");
            conn.method(Method.GET);
            conn.followRedirects(false);
            Response response;
            response = conn.execute();
            Map getCookies = response.cookies();
            String Cookie = getCookies.toString();
            Cookie = Cookie.substring(Cookie.indexOf("{")+1, Cookie.lastIndexOf("}"));
            Cookie = Cookie.replaceAll(",", ";");
            return Cookie;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    public static String goHttp() {

        try {
            Connection conn = Jsoup.connect("https://xueqiu.com/v4/stock/quote.json?code=SH600000");
            conn.header("Host","xueqiu.com");
            conn.header("Connection","keep-alive");
            conn.header("Cache-Control","max-age=0");
            conn.header("Upgrade-Insecure-Requests","1");
            conn.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            conn.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//          conn.header("Accept-Encoding","gzip, deflate, sdch, br");
            conn.header("Accept-Language","zh-CN,zh;q=0.8");
            conn.header("Cookie",returnCookies());
            conn.method(Method.GET);
            conn.followRedirects(false);
            conn.ignoreContentType(true);
            Response response = conn.execute();
            String body = response.body(); 
            System.out.println(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}

좋은 웹페이지 즐겨찾기