获取cookie后将cookie放在下一次请求

获取cookie后将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<String, String> 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;

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值