首页 技术 正文
技术 2022年11月16日
0 收藏 571 点赞 4,029 浏览 1590 个字

java.net.URL 模拟用户登录网页并维持session

半成品,并非完全有用

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;public class SessionHolder {
public static void main(String[] args) throws Exception {
URL url = new URL("http://192.168.1.147:144/platformproxy/j_spring_security_check");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);// 允许连接提交信息
connection.setRequestMethod("POST");// 网页提交方式“GET”、“POST”
connection.setRequestProperty("User-Agent", "Mozilla/4.7 [en] (Win98; I)");
StringBuffer sb = new StringBuffer();
// sb.append("username=admin");
// sb.append("&password=admin");
sb.append("j_username=09000001");
sb.append("&j_password=zsinsgooda");
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("网页的登录后的页面");
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();
}
}
}

引用自:http://blog.csdn.net/zgrjkflmkyc/article/details/9150511

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862