如何从服务器获取所有cookie?

时间:2018-12-25 18:11:25

标签: java cookies httpclient

我正在尝试使用Apache HttpClient为Hackerrank网站创建API。但是,在登录时,服务器不会发送一个包含用于进一步操作的ID的关键cookie。我尝试设置用户代理。但是,所需的cookie会在访问登录页面时进入浏览器,正如我在firefox开发工具的“存储”选项卡中看到的那样。服务器有什么期望吗?我很想学习,但是我不确定我应该用谷歌搜索什么关键字。我想学习网站如何区分实际用户请求和其他请求。我想学习如何模仿实际用户的请求,哪些浏览器能够发送到网站。

我曾尝试根据高级开发人员之一的建议为网站设置SSL证书,并在请求标头中设置用户代理。

System.out.println("starting");
    CookieStore httpCookieStore = new BasicCookieStore();
    CookieStore backupCookieStore = new BasicCookieStore();
    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore);

    HttpClient client = HttpClients.custom().setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36").build();


    HttpClientContext context = new HttpClientContext();
    HttpPost post = new HttpPost("https://www.hackerrank.com/auth/login");
    HttpPost submitPost = new HttpPost("https://www.hackerrank.com/rest/contests/master/challenges/30-hello-world/compile_tests");


    HttpGet prelogin = new HttpGet("https://www.hackerrank.com/login?");
    HttpGet get = new HttpGet("https://www.hackerrank.com/dashboard");

    try {
        System.out.println("doing ");
        HttpResponse  preResponse = client.execute(prelogin,context);
          Header[] preheaders = preResponse.getAllHeaders();

            System.out.println(":: prelogin >>");
            for (Header header : preheaders) {
                System.out.println(header.getName() +" : "+header.getValue());
            }
            httpCookieStore = context.getCookieStore();
            List<Cookie> cookies = httpCookieStore.getCookies();

            System.out.println("");
            System.out.println(":: prelogin cookies >>");
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() +" : "+cookie.getValue());
            }



     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("login", "Username"));
        nameValuePairs.add(new BasicNameValuePair("password", "Password"));
        nameValuePairs.add(new BasicNameValuePair("remember_me", "false"));
        nameValuePairs.add(new BasicNameValuePair("fallback","true"));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse  response = client.execute(post,context);

        Header[] headers = response.getAllHeaders();
        System.out.println(":: headers >>");
        for (Header header : headers) {
            //System.out.println(header.getName() +" : "+header.getValue());
        }
        httpCookieStore = context.getCookieStore();
        cookies = httpCookieStore.getCookies();

        System.out.println("");
        System.out.println(":: cookies >>");
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() +" : "+cookie.getValue());
        }

我希望在访问URL时会得到firefox或chrome正在获取的所有cookie。我希望模仿确切的请求,将这些浏览器发送到网站的内容。

0 个答案:

没有答案