import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class JsoupDemo {
/*
* pagePath 要填写用户名,密码的页面
* name 用户名
* password 密码
*/
public static Document login(String pagePath,String name,String password){
try {
//pagePath=http://eol.jxau.edu.cn/meol/homepage/common/index.jsp
Response rs=Jsoup.connect(pagePath).method(Method.GET).execute();
Document rd=rs.parse();
//获取form表单的action=/meol/loginCheck.do
String actionPath=rd.select("form").attr("action");
//获取action中第一个单词meol
String firstWordInAction=actionPath.split("[/]")[1];
int index=pagePath.indexOf(firstWordInAction);
// connectPath=http://eol.jxau.edu.cn/meol/loginCheck.do
String connectPath=pagePath.substring(0, index-1)+actionPath;
//保存用户名,密码
Map<String,String> datas=new HashMap<String, String>();
datas.put("IPT_LOGINUSERNAME", name);
datas.put("IPT_LOGINPASSWORD", password);
//执行post 执行成功返回Document对象
Document document=Jsoup.connect(connectPath).data(datas).post();
System.out.println(document);
return document;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
login("http://eol.jxau.edu.cn/meol/homepage/common/index.jsp", "", "");
}
}
上面的代码就是使用Jsoup实现登录
Jsoup的jar包下载地址: 点击打开链接