I've copied Mkyong's Apache HttpClient 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!): Mkyong's example
private void sendPost(String url, List<NameValuePair> 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());
}
The print statement at the end of the method returns that the page has been 'moved temporarily'.
<HTML><HEAD><TITLE>Moved Temporarily</TITLE></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"><H1>Moved Temporarily</H1>The document has moved <A HREF="https://accounts.google.com/CheckCookie?chtml=LoginDoneHtml&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount&gidl=CAA">here</A>.</BODY></HTML>
I believe this is a redirect, though I am not entirely sure. Here is the main method:
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<NameValuePair> 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);
}
The code does not process past String result = http.GetPageContent(gmail);
I believe that this has something to do with the issue (this is from Handling HttpClient Redirects)
10.3.3 302 Found ...
If the 302 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
I have attempted to override the isRedirected()
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 POST cases?
I am unsure if this is the entire problem and am unsure if I am approaching this correctly at all.
However, I would appreciate any explanations that would help me understand the process of logging into a website using Httpclient.