模拟登录中,如何保持session
用java写的一个模拟登录程序,转到userInfo 页面时却得不到用户信息,怀疑是得不到session。
贴上代码,像这样先取得cookie 再发回去的方法好像不可行?
public static void login() throws HttpException, IOException{
URL url = new URL("http://localhost/ONCAPS/login.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);//允许连接提交信息
connection.setRequestMethod("POST");//网页提交方式“GET”、“POST”
connection.setRequestProperty("User-Agent", "Mozilla/4.7 en");
StringBuffer sb = new StringBuffer();
sb.append("user_id=test1");
sb.append("password=123456");
OutputStream os = connection.getOutputStream();
os.write(sb.toString().getBytes());
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie
System.out.println("cookie:" + responseCookie);
String line = br.readLine();
while (line != null) {
System.out.println(new String(line.getBytes()));
line = br.readLine();//打出登录的网页
}
//acces
URL url1 = new URL("http://localhost/ONCAPS/userInfo.php");
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
connection1.setRequestProperty("Cookie", responseCookie);//给服务器送登录后的cookie
BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));
String line1= br1.readLine();
while (line1 != null) {
System.out.println(new String(line1.getBytes()));
line1 = br1.readLine();
}
}
谢谢各位
贴上代码,像这样先取得cookie 再发回去的方法好像不可行?
public static void login() throws HttpException, IOException{
URL url = new URL("http://localhost/ONCAPS/login.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);//允许连接提交信息
connection.setRequestMethod("POST");//网页提交方式“GET”、“POST”
connection.setRequestProperty("User-Agent", "Mozilla/4.7 en");
StringBuffer sb = new StringBuffer();
sb.append("user_id=test1");
sb.append("password=123456");
OutputStream os = connection.getOutputStream();
os.write(sb.toString().getBytes());
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie
System.out.println("cookie:" + responseCookie);
String line = br.readLine();
while (line != null) {
System.out.println(new String(line.getBytes()));
line = br.readLine();//打出登录的网页
}
//acces
URL url1 = new URL("http://localhost/ONCAPS/userInfo.php");
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
connection1.setRequestProperty("Cookie", responseCookie);//给服务器送登录后的cookie
BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));
String line1= br1.readLine();
while (line1 != null) {
System.out.println(new String(line1.getBytes()));
line1 = br1.readLine();
}
}
谢谢各位
...全文
相关推荐
httpclient如何保持session会话模拟登录后的操作
NULL 博文链接:https://shaozhen.iteye.com/blog/946680
Session 会话保持:模拟登录后持续访问需要权限的页面
本文深入解析了Python中Session会话保持技术的实现与应用。通过requests库的Session对象,可自动管理Cookie实现状态维持,有效解决模拟登录及权限访问问题。文章详细阐述了Session工作原理与Cookie的协同机制,并提供了完整的实战流程:从动态参数获取、表单登录提交到多权限页面访问。关键技术包括CSRFToken处理、会话失效检测与自动重连机制等。通过案例演示,验证了会话保持的可行性,为开发者在爬虫应用中处理复杂认证场景提供了实用解决方案。掌握这些技术可显著提升程序在需身份验证环
python使用requests.session模拟登录
最近开发一套接口,写个Python脚本,使用requests.session模拟一下登录.本文通过实例代码给大家介绍python使用requests.session模拟登录,感兴趣的朋友跟随小编一起看看吧
java中使用session模拟用户登陆
java中使用session模拟用户登陆
session模拟数据库实现用户登录功能
模拟数据库,以session实现用户登陆功能,配套对应完整登陆注册页面!


Web 开发
