Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible redirect issue while trying to log in to website with HttpClient
    text
    copied!<p>I've copied <strong><em>Mkyong's Apache HttpClient</em></strong> example almost word for word except for swapping out the deprecated methods and my own log in information (I even managed to copy his typos!): <a href="http://www.mkyong.com/java/apache-httpclient-examples/" rel="nofollow noreferrer">Mkyong's example</a></p> <pre><code>private void sendPost(String url, List&lt;NameValuePair&gt; postParams) throws Exception { post.setParams(params); post.setHeader("Host", "accounts.google.com"); post.setHeader("User-Agent", USER_AGENT); post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); post.setHeader("Accept-Language", "en-US,en;q=0.5"); post.setHeader("Cookie", getCookies()); post.setHeader("Connection", "keep-alive"); post.setHeader("Referer", "https://accounts.google.com/ServiceLoginAuth"); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setEntity(new UrlEncodedFormEntity(postParams)); HttpResponse response = client.execute(post); int responseCode = response.getStatusLine().getStatusCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + postParams); System.out.println("Response Code : " + responseCode); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } System.out.println(result.toString()); } </code></pre> <p>The print statement at the end of the method returns that the page has been 'moved temporarily'.</p> <p><code>&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Moved Temporarily&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;&lt;H1&gt;Moved Temporarily&lt;/H1&gt;The document has moved &lt;A HREF="https://accounts.google.com/CheckCookie?chtml=LoginDoneHtml&amp;amp;continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&amp;amp;gidl=CAA"&gt;here&lt;/A&gt;.&lt;/BODY&gt;&lt;/HTML&gt;</code></p> <p>I believe this is a redirect, though I am not entirely sure. Here is the main method:</p> <pre><code>public static void main(String[] args) throws Exception { String url = "https://accounts.google.com/ServiceLoginAuth"; String gmail = "https://mail.google.com/mail/"; CookieHandler.setDefault(new CookieManager()); HttpCilentExample http = new HttpCilentExample(); String page = http.GetPageContent(url); List&lt;NameValuePair&gt; postParams = http.getFormParams(page, "example@gmail.com","examplePassword"); http.sendPost(url, postParams); System.out.println("past here"); String result = http.GetPageContent(gmail); System.out.println(result); } </code></pre> <p>The code does not process past <code>String result = http.GetPageContent(gmail);</code></p> <p>I believe that this has something to do with the issue (this is from <a href="https://stackoverflow.com/questions/5169468/handling-httpclient-redirects">Handling HttpClient Redirects</a>)</p> <blockquote> <p>10.3.3 302 Found ...</p> <p>If the 302 status code is received in response to a request other<br> than GET or HEAD, the user agent MUST NOT automatically redirect the<br> request unless it can be confirmed by the user, since this might<br> change the conditions under which the request was issued.</p> </blockquote> <p>I have attempted to override the <code>isRedirected()</code> method, but am unsure as to what I am supposed to do. Am I to return false for all cases, or check the parameters for <strong><em>POST</em></strong> cases?</p> <p>I am unsure if this is the entire problem and am unsure if I am approaching this correctly at all.</p> <p>However, I would appreciate any explanations that would help me understand the process of logging into a website using <strong><em>Httpclient</em></strong>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload