How to automate login a website – Java example
来源:互联网 发布:matlab取矩阵中的元素 编辑:程序博客网 时间:2024/05/16 10:11
In this example, we will show you how to login a website via standard Java HttpsURLConnection
. This technique should be working in most of the login form.
Tools & Java Library used in this example
- Google Chrome Browser – Network tab to analyze HTTP request and response header fields.
- jsoup library – Extracts HTML form values.
- JDK 6.
1. Analyze Http Headers, form data.
To login a website, you need to know following values :
- Login form URL.
- Login form data.
- URL for authentication.
- Http request / response header.
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 :
- Send an HTTP “GET” request to Google login form – https://accounts.google.com/ServiceLoginAuth
- Analyze the form data via Google Chrome’s “Network” feature. Alternatively, you can view the HTML source code.
- Use jSoup library to extract all visible and hidden form’s data, replace with your username and password.
- Send a HTTP “POST” request back to login form, along with the constructed parameters
- After user authenticated, send another HTTP “GET” request to Gmail page. https://mail.google.com/mail/
Note
This example is just to show you the capability and functionality of Java HttpURLConnection. In general, you should use theGoogle Gmail API to interact with Gmail.
This example is just to show you the capability and functionality of Java HttpURLConnection. In general, you should use theGoogle 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;public class HttpUrlConnectionExample { private List<String> cookies; 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 onCookieHandler.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// authenticationhttp.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 browserconn.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 requestDataOutputStream 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 GETconn.setRequestMethod("GET");conn.setUseCaches(false);// act like a browserconn.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 cookiessetCookies(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 idElement loginform = doc.getElementById("gaia_loginform");Elements inputElements = loginform.getElementsByTag("input");List<String> paramList = new ArrayList<String>();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 listStringBuilder result = new StringBuilder();for (String param : paramList) {if (result.length() == 0) {result.append(param);} else {result.append("&" + param);}}return result.toString(); } public List<String> getCookies() {return cookies; } public void setCookies(List<String> cookies) {this.cookies = cookies; }}
Output
Sending 'GET' request to URL : https://accounts.google.com/ServiceLoginAuthResponse Code : 200Extracting form data...Sending 'POST' request to URL : https://accounts.google.com/ServiceLoginAuthPost parameters : dsh=-293322094146108856&GALX=CExqdUbvEr4&timeStmp=&secTok=&_utf8=%E2%98%83&bgresponse=js_disabled&Email=username&Passwd=password&signIn=Sign+in&PersistentCookie=yes&rmShown=1Response Code : 200Sending 'GET' request to URL : https://mail.google.com/mail/Response Code : 200<!-- gmail page content.....-->
Note
Refer to this equivalent example, but using Apache HttpClient to send HTTP request.
Refer to this equivalent example, but using Apache HttpClient to send HTTP request.
0 0
- How To Automate Login A Website – Java Example
- How to automate login a website – Java example
- How to Copy a Website
- How to Login Automatically into Website Using Excel VBA
- HOW TO configure a website for HTTPS?
- BibTeX: How to cite a website
- How to Make a Single Page Website
- How To Automate Disconnection of Idle Sessions
- How to read file in Java – BufferedInputStream Example
- Java Deadlock Example – How to analyze deadlock situation
- How To Login To Windows Without A Password
- How to Login ODI
- QT12 How to open a new window after successful Login
- How to make a simple login system with PHP
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- How to monitor your website
- Java – How to split a string
- How to use epoll? A complete example in C
- BZOJ 1070, 修车
- 技术人的职场晋升指南:如何从20万到50万,再跨越到100万?大咖说回放
- Git,Github和Gitlab简介和使用方法
- How to send HTTP request GET/POST in Java
- iOS基于JSPatch的热修复
- How to automate login a website – Java example
- Upload file to servlet without using HTML form
- 来CSDN必做功课:阅读 zhao4zhong1 和 sp1234
- empty运算符
- 技术人的小目标:10000小时理论落地,你就是大牛
- Upload files by sending multipart request programmatically
- 程序员简易成长指南:从菜鸟码农到架构师
- 左耳朵耗子:不灌鸡汤,说真的年龄渐长,技术人的发展之路该怎么走?
- 天天写「业务代码」,如何成为「技术大牛」?