Beruflich Dokumente
Kultur Dokumente
1. Google Chrome Browser Network tab to analyze HTTP request and response header
fields.
3. JDK 6.
Uses Google Chrome to get above data. In Chrome, right click everywhere, choose Inspect
Element -> Network tab.
Before you code, try login via Chrome, observe how the HTTP request, response and form data
works, later you need to simulate the same steps in Java.
2. HttpsURLConnection Example
In this example, we show you how to login Gmail.
Summary :
2. Analyze the form data via Google Chromes Network feature. Alternatively, you can view
the HTML source code.
3. Use jSoup library to extract all visible and hidden forms data, replace with your username
and password.
4. Send a HTTP POST request back to login form, along with the constructed parameters
Note
This example is just to show you the capability and functionality of Java HttpURLConnection. In
general, you should use the Google Gmail API to interact with Gmail.
HttpUrlConnectionExample.java
package com.mkyong;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
CookieHandler.setDefault(new CookieManager());
// 1. Send a "GET" request, so that you can extract the form's data.
// 2. Construct above post's content and then send a POST request for
// authentication
http.sendPost(url, postParams);
System.out.println(result);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "accounts.google.com");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0
.8");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Referer",
"https://accounts.google.com/ServiceLoginAuth");
conn.setRequestProperty("Content-Type", "application/x-www-form-
urlencoded");
conn.setRequestProperty("Content-Length",
Integer.toString(postParams.length()));
conn.setDoOutput(true);
conn.setDoInput(true);
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
BufferedReader in =
String inputLine;
response.append(inputLine);
in.close();
// System.out.println(response.toString());
// default is GET
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0
.8");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
if (cookies != null) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)
[0]);
BufferedReader in =
String inputLine;
response.append(inputLine);
in.close();
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
throws UnsupportedEncodingException {
// Google form id
if (key.equals("Email"))
value = username;
else if (key.equals("Passwd"))
value = password;
if (result.length() == 0) {
result.append(param);
} else {
result.append("&" + param);
return result.toString();
return cookies;
}
this.cookies = cookies;
Output
&bgresponse=js_disabled&Email=username&Passwd=password&signIn=Sign+in&Persiste
ntCookie=yes&rmShown=1