用java 自动登录一个网站的例子
By:Roy.LiuLast updated:2013-06-24
在这个例子中,我将用java的HttpURLConnection,去登录一个web 站点。用这种方法的时候,一般是在一个form 中登录的。
在这个例子中,用到如下工具 :
1. Google chrome 浏览器。
2. jsoup 库,用来提取html form 表单中的值. (你可以在这里下载 http://jsoup.org/ )
3. jdk 6.
一。分析http header,form data
要登录一个网站,必须知道的几件事:
1. 登录的 URL
2. 登录所需要的数据
3. 认证的URL
4. Http request/response header.
用chrome 浏览器打开上述页面,然后右键点击页面,可以看到 "查看元素" inspect element, 然后选择 网络 network Tab 页。首先打开gooogle 网站,并尝试登录,查看http request,response 数据,在后面我们会模拟这些数据.

二. 用 HttpsURLConnection 完成demo
1. 发送 http get 请求 到 google 的登录form: https://accounts.google.com/ServiceLoginAuth
2. 通过google 浏览器分析 网络 tab 页面,
3. 利用jsoup 得到form 里面隐藏的数据,然后放入自己的username 和 password
4. 发送post 请求
5. 认证完毕,发送另外一个请求到gmail 页面。(这里仅仅是例子,如果是为了访问gmail ,可以直接利用google 提供的 GMAIL API 去完成)
输出
在这个例子中,用到如下工具 :
1. Google chrome 浏览器。
2. jsoup 库,用来提取html form 表单中的值. (你可以在这里下载 http://jsoup.org/ )
3. jdk 6.
一。分析http header,form data
要登录一个网站,必须知道的几件事:
1. 登录的 URL
2. 登录所需要的数据
3. 认证的URL
4. Http request/response header.
用chrome 浏览器打开上述页面,然后右键点击页面,可以看到 "查看元素" inspect element, 然后选择 网络 network Tab 页。首先打开gooogle 网站,并尝试登录,查看http request,response 数据,在后面我们会模拟这些数据.

二. 用 HttpsURLConnection 完成demo
1. 发送 http get 请求 到 google 的登录form: https://accounts.google.com/ServiceLoginAuth
2. 通过google 浏览器分析 网络 tab 页面,
3. 利用jsoup 得到form 里面隐藏的数据,然后放入自己的username 和 password
4. 发送post 请求
5. 认证完毕,发送另外一个请求到gmail 页面。(这里仅仅是例子,如果是为了访问gmail ,可以直接利用google 提供的 GMAIL API 去完成)
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; public class HttpUrlConnectionExample { private Listcookies; private HttpsURLConnection conn; private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { String url = "https://accounts.google.com/ServiceLoginAuth"; String gmail = "https://mail.google.com/mail/"; HttpUrlConnectionExample http = new HttpUrlConnectionExample(); // make sure cookies is turn on CookieHandler.setDefault(new CookieManager()); // 1. Send a "GET" request, so that you can extract the form's data. String page = http.GetPageContent(url); String postParams = http.getFormParams(page, "username@gmail.com", "password"); // 2. Construct above post's content and then send a POST request for // authentication http.sendPost(url, postParams); // 3. success then go to gmail. String result = http.GetPageContent(gmail); System.out.println(result); } private void sendPost(String url, String postParams) throws Exception { URL obj = new URL(url); conn = (HttpsURLConnection) obj.openConnection(); // Acts like a browser 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"); for (String cookie : this.cookies) { conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]); } 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); // Send post request DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(postParams); wr.flush(); wr.close(); int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + postParams); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // System.out.println(response.toString()); } private String GetPageContent(String url) throws Exception { URL obj = new URL(url); conn = (HttpsURLConnection) obj.openConnection(); // default is GET conn.setRequestMethod("GET"); conn.setUseCaches(false); // act like a browser 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) { for (String cookie : this.cookies) { conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]); } } int responseCode = conn.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Get the response cookies setCookies(conn.getHeaderFields().get("Set-Cookie")); return response.toString(); } public String getFormParams(String html, String username, String password) throws UnsupportedEncodingException { System.out.println("Extracting form's data..."); Document doc = Jsoup.parse(html); // Google form id Element loginform = doc.getElementById("gaia_loginform"); Elements inputElements = loginform.getElementsByTag("input"); List paramList = new ArrayList (); for (Element inputElement : inputElements) { String key = inputElement.attr("name"); String value = inputElement.attr("value"); if (key.equals("Email")) value = username; else if (key.equals("Passwd")) value = password; paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8")); } // build parameters list StringBuilder result = new StringBuilder(); for (String param : paramList) { if (result.length() == 0) { result.append(param); } else { result.append("&" + param); } } return result.toString(); } public List getCookies() { return cookies; } public void setCookies(List cookies) { this.cookies = cookies; } }
输出
Sending 'GET' request to URL : https://accounts.google.com/ServiceLoginAuth Response Code : 200 Extracting form data... Sending 'POST' request to URL : https://accounts.google.com/ServiceLoginAuth Post parameters : dsh=-293322094146108856&GALX=CExqdUbvEr4&timeStmp=&secTok=&_utf8=%E2%98%83 &bgresponse=js_disabled&Email=username&Passwd=password&signIn=Sign+in&PersistentCookie=yes&rmShown=1 Response Code : 200 Sending 'GET' request to URL : https://mail.google.com/mail/ Response Code : 200
From:一号门
Previous:再次回到家乡,看到生机勃勃的景象
COMMENTS