关于爬虫模拟登陆

qq_33190755 qq_33190755 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我是用的是OKHTTP,想模拟登陆学校的教务系统,我看了下网页源码,他提交表单之后先重定向到一个网址,然后那个网址又重定向到另一个网址 ,然后它再get到个人信息页面,我模拟了表单提交的情况,然后他就一直返回登陆页面的源码,这是啥情况?代码贴下:
public class HttpDemo {


/**
* 主函数,测试请求,提交表单
*
* @param args
*/
public static void main(String[] args) {
// HttpURLConnectionDemo httpURLConnectionDemo = new HttpURLConnectionDemo();
JsoupDemo demo = new JsoupDemo();
List<String> lists = demo.jsoupDemo();
String username = "150******";
String password = "07****";
String lt = lists.get(0);
String execution = lists.get(1);
String _eventId = lists.get(2);
String rmShow = lists.get(3);
OKhttpDemo oKhttpDemo = new OKhttpDemo();
String cookie = null;
try {
cookie = oKhttpDemo.getCookie();
}catch (Exception e){
e.printStackTrace();
}


oKhttpDemo.sendPostAsync(username,password,lt,execution,_eventId,rmShow,cookie);



}


上面这段是模拟提交的主程序


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

/**
* 通过Jsoup获取表单中隐藏域的值
*/
public class JsoupDemo {
public List<String> jsoupDemo(){
List<String> list = new ArrayList<>();
String url=
"http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal";
try {
//解析URL获得document对象,相当于点击去了URL
Document document = Jsoup.connect(url).timeout(10000).get();
//获取网页源码中id为"casLoginFrom"的标签
Element element = document.getElementById("casLoginForm");
//选择标签内有type属性为hidden的标签
Elements elements = document.getElementsByAttributeValue("type","hidden");
//System.out.println(elements);
//获取标签的value值,并存入list集合
for (Element ele :elements) {
String value = ele.attr("value");
list.add(value);
//System.out.println(value);

}
} catch (IOException e) {
e.printStackTrace();
}

return list;
}
}


上面这个是获取隐藏域的值




import okhttp3.*;

import java.io.IOException;

public class OKhttpDemo {

/**
* 获取第一次访问的Cookie
* @return
* @throws IOException
*/
public String getCookie() throws IOException{

String url1="http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal";

OkHttpClient c=new OkHttpClient();

Request req=new Request.Builder().url(url1).get().build();

Response response=c.newCall(req).execute();

String Set_Cookie = response.header("Set-Cookie");

String str = Set_Cookie.substring(0, Set_Cookie.indexOf(";"));

System.out.println(str);

return str;
}


/**
* 添加请求头信息,模拟登陆
* @param username
* @param password
* @param lt
* @param execution
* @param _eventId
* @param rmShow
* @param cookie
*/
public void sendPostAsync(String username,String password,String lt,String execution, String _eventId, String rmShow,String cookie){

OkHttpClient client=new OkHttpClient();

//创建一个表单对象

FormBody formBody=new FormBody.Builder()

//添加表单内容

.add("username",username)

.add("password",password)

.add("lt",lt)

.add("execution",execution)

.add("_eventId",_eventId)

.add("rmShow",rmShow)

.build();

//创建一个请求

Request request=new Request.Builder()

//添加url

.url("http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal")

//添加请求头
.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")

.addHeader("Accept-Encoding","gzip, deflate")

.addHeader("Accept-Language","zh-CN,zh;q=0.9")

.addHeader("Cache-Control","no-cache")

.addHeader("Connection","keep-alive")

.addHeader("Content-Type","application/x-www-form-urlencoded")

.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36")

.addHeader("Origin"," http://ids.wbu.edu.cn")

.addHeader("Referer","http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal")

.addHeader("Cookie",cookie)

//设置请求对象

.post(formBody).build();

Call call=client.newCall(request);

call.enqueue(new Callback(){

//这里是异步操作\

//请求失败时调用

@Override

public void onFailure(Call p1, IOException p2){

// TODO: Implement this method

p2.printStackTrace();

}

@Override

public void onResponse(Call p1, Response p2) throws IOException{

// TODO: Implement this method

//获取响应内容

String responseContent=p2.body().string();

System.out.println(responseContent);

}

});

}

}


上面这个是模拟登陆的逻辑


这是登陆页面信息

这是第一次重定向网址

这是第一次重定向传递的参数

这是第一次重定向的消息内容

这是第二次重定向的网址

这是第二次重定向的消息内容

这是最后get到的个人信息的页面



...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
qq_33190755
Java SE 62,621 成员 307,251 内容