HttpClient错误:请尝试启用Cookie或关闭隐私模式

时间:2015-06-23 03:54:11

标签: java login oauth-2.0 httpclient instagram-api

我尝试OAuth使用HttpClient(4.3)登录Instagram,但收到以下错误消息:

  

无法加载此页面。如果您的cookie已被禁用   浏览器,或者您正在私人模式下浏览,请尝试启用   Cookie或关闭私密模式,然后重试您的操作。

我花了三天的时间尝试各种方法来设置后续请求中的Cookie,但我仍然没有解决这个问题!

谁能帮帮我?

我的操作步骤:

1. Get login page with HttpGet.
2. Parse Cookie from response.
3. Parse form action from response.
4. Parse form fields from response.
5. Set Cookie to HttpPost.
6. Set form fields to HttpPost.
7. Post the form.
8. Get the error message.

代码:

  1. Init HttpClient

        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
        CookieStore cookieStore = new BasicCookieStore();
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build();
    
  2. 发送获取登录页面请求:

        HttpGet get = new HttpGet(url);
        get.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");
    
        HttpResponse response = httpClient.execute(get);
    
  3. 获取Cookie:

    private Map<String, String> getCookies(HttpResponse response) {
        Map<String, String> cookieMap = new HashMap<String, String>();
        Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            if (headers[i].getName().equals("Set-Cookie")) {
                String cookie = headers[i].getValue();
                String[] pairs = cookie.split(";");
                for(String pair: pairs){
                    String[] keyValue = pair.split("=");
                    String cookieName = keyValue[0];
                    String cookieValue = keyValue[1];
                    cookieMap.put(cookieName, cookieValue);
                }
            }
        }
        return cookieMap;
    }
    
  4. 获取表单字段:

    // org.htmlparser.Parser
    private static Map<String, String> getFormFields(NodeList list,
        String keyword) {
    
        Map<String, String> responeMap = new HashMap<String, String>();
        SimpleNodeIterator iterator = list.elements();
        while (iterator.hasMoreNodes()) {
            Node node = iterator.nextNode();
            NodeList childList = node.getChildren();
            if (null == childList) {
                if (node instanceof InputTag) {
                    InputTag d = (InputTag) node;
                    if (d.getAttribute("name") != null)
                        responeMap.put(d.getAttribute("name"),
                            d.getAttribute("value"));
                }
            } else {
                if(node instanceof FormTag){
                    FormTag d = (FormTag)node;
                    loginAction  = d.getAttribute("action");
                }
                responeMap.putAll(processNodeList(childList, keyword));
            }
        }
    
        return responeMap;
    }
    
  5. 发表表格:

        HttpPost post = new HttpPost(loginUrl);
    
        post.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");
        if (cookieMap.size() > 0) {
            post.setHeader("Cookie", buildCookies(cookieMap));
        }
    
        UrlEncodedFormEntity uef = new UrlEncodedFormEntity(pairs, "UTF-8");
        post.setEntity(uef);
    
        response = httpClient.execute(post);
    
  6. 获取错误消息

  7.   

    无法加载此页面。如果您的cookie已被禁用   浏览器,或者您正在私人模式下浏览,请尝试启用   Cookie或关闭私密模式,然后重试您的操作。

1 个答案:

答案 0 :(得分:1)

我也花了一些时间在这项任务上,我通过自己处理所有标题和烹饪解决了这个问题,因为httpClient无法正确完成。 我的POST方法示例:

public String sendPostLoginRequest(){
    String htmlResponse = "";
    String loc = "";
    String url = "https://instagram.com"+action;
    System.out.println(url);
    httpPost = new HttpPost(url);
    httpPost.setHeader("Host", "instagram.com");
    httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0");
    httpPost.setHeader("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    httpPost.setHeader("Accept-Language", "en-US,en;q=0.5");
    httpPost.setHeader("Cookie", cook);
    httpPost.setHeader("Connection", "keep-alive");
    httpPost.setHeader("Referer", "https://instagram.com/accounts/login/?force_classic_login=&next=/oauth/authorize/%3Fclient_id%HERE_SHOULD_BE_CLIENT_ID%26redirect_uri%3Dhttp%3A//localhost%26response_type%3Dcode");
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(hidenName, hidenValue));
    nvps.add(new BasicNameValuePair("username", login));
    nvps.add(new BasicNameValuePair("password", password));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response2 = httpclient.execute(httpPost);
        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();
        Header headerLoc = response2.getFirstHeader("Location");


        BufferedReader bf = new BufferedReader(new InputStreamReader(entity2.getContent()));

        while (bf.ready()){
            String s = bf.readLine();
            htmlResponse = htmlResponse + s;
            System.out.println(s);
        }
        setCookies(response2.getFirstHeader("Set-Cookie") == null ? "" :
                response2.getFirstHeader("Set-Cookie").toString());
        loc = headerLoc.getValue();
        System.out.println(loc);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return loc;
}

我改变变量&#34;做饭&#34;每次请求后。 HttpClient通常会获得第一个cookie,并且在下一个当前域请求后不会更改它。此外,HttpClientSo不处理重定向,因此您应该编写e

请求