注:本文仅仅属于个人记录文档。
一、需要引入jsoup的jar包
二、login.java的代码
说明: trustEveryone()方法完全引用自该链接:http://espace.iteye.com/blog/1917574
package com.jsoup;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class login {
//解决https访问的信任证书
public static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
// e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
trustEveryone();
Connection con = Jsoup.connect("https://localhost/exam/login");
//con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
Response rs = con.execute();
Document doc = Jsoup.parse(rs.body());
//System.out.println(rs.url());
//System.out.println(doc.body());
List<Element> et = doc.select("#login_form");
Map<String, String> datas = new HashMap<>();
for(Element e : et.get(0).getAllElements()){
if(e.attr("name").equals("username")){
e.attr("value","zhangsan");
}
if(e.attr("name").equals("password")){
e.attr("value","password");
}
if(e.attr("name").length()>0){
datas.put(e.attr("name"), e.attr("value"));
}
}
Connection con2=Jsoup.connect("https://localhost/exam/login");
con2.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
rs = con2.ignoreContentType(true)
.method(Method.POST)
.data(datas)
.cookies(rs.cookies())
.execute();
Map<String, String> cook = rs.cookies();
/*Document document = Jsoup.connect("https://localhost/exam/...").data(datas).cookies(rs.cookies()).post();
System.out.println(document.body());
// Map<String, String> map = login.cookies();
for(String s:map.keySet()){
System.out.println(s+" "+ map.get(s));
}*/
rs = Jsoup.connect("https://localhost/exam/users/")
.ignoreContentType(true)
.method(Method.GET)
.cookies(cook)
.execute();
doc = Jsoup.parse(rs.body());
System.out.println(doc.select("#username").val());
datas = new HashMap<String, String>();
datas.put("username", doc.select("#username").val());
datas.put("password", doc.select("#password").val());
rs = Jsoup.connect("https://localhost/exam/users/")
.ignoreContentType(true)
.method(Method.POST)
.cookies(cook)
.data(datas)
.execute();
System.out.println(rs.parse());
/*Connection con3=Jsoup.connect("https://localhost/exam/users");
con2.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
Response login2 = con3.ignoreContentType(true)
.method(Method.POST)
.cookies(login.cookies())
.execute();
System.out.println(login2.body());*/
/* con3=Jsoup.connect("https://localhost/exam/...");
Map<String, String> date = new HashMap<>();
date.put("questionid", "40");
date.put("type", "type03");
Response login3 = con3.ignoreContentType(true)
.method(Method.GET)
.cookies(login.cookies())
//.data(date)
.execute();
System.out.println(login3.body());*/
/*
Map<String, String> map = new HashMap<>();
map.put("username", "username");
map.put("password", "password");
Document doc = Jsoup.connect("https://localhost/exam/...").data(map).get();
System.out.println(doc.body());*/
}
}