title: HttpClient Error: Please try enabling cookies or turning off Private Mode
body: <p>I tried to <code>OAuth</code> Login Instagram with <code>HttpClient(4.3)</code>, but got the following <strong>error message:</strong> </p>
  
  <blockquote>
    <p>This page could not be loaded. If you have cookies disabled in your
    browser, or you are browsing in Private Mode, please try enabling
    cookies or turning off Private Mode, and then retrying your action.</p>
  </blockquote>
  
  <p>I have spent three days to try all ways I can do to set cookies in post request, but I still didn't solve this problem!</p>
  
  <p>Who can help me?</p>
  
  <p><strong>My operation steps:</strong></p>
  
  <pre><code>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.
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <ol>
  <li><p><code>Init HttpClient</code>:</p>
  
  <pre><code>    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();
  </code></pre></li>
  <li><p>Send get Login page request:</p>
  
  <pre><code>    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);
  </code></pre></li>
  <li><p>Get Cookie:</p>
  
  <pre><code>private Map&lt;String, String&gt; getCookies(HttpResponse response) {
      Map&lt;String, String&gt; cookieMap = new HashMap&lt;String, String&gt;();
      Header[] headers = response.getAllHeaders();
      for (int i = 0; i &lt; 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;
  }
  </code></pre></li>
  <li><p>Get form fields:</p>
  
  <pre><code>// org.htmlparser.Parser
  private static Map&lt;String, String&gt; getFormFields(NodeList list,
      String keyword) {
  
      Map&lt;String, String&gt; responeMap = new HashMap&lt;String, String&gt;();
      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;
  }
  </code></pre></li>
  <li><p>Post form:</p>
  
  <pre><code>    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() &gt; 0) {
          post.setHeader("Cookie", buildCookies(cookieMap));
      }
  
      UrlEncodedFormEntity uef = new UrlEncodedFormEntity(pairs, "UTF-8");
      post.setEntity(uef);
  
      response = httpClient.execute(post);
  </code></pre></li>
  <li><p>Get error message</p></li>
  </ol>
  
  <blockquote>
    <p>This page could not be loaded. If you have cookies disabled in your
    browser, or you are browsing in Private Mode, please try enabling
    cookies or turning off Private Mode, and then retrying your action.</p>
  </blockquote>