java HttpURLConnection 登录网站 完整代码
import java.io.*;
import java.util.*;
import java.net.*; public class WebTest { public static void main(String[] args) { System.out.println("beging...");
DownLoadPages("http://login.xiaonei.com/Login.do", "d:/fileDown.txt");
// visit("http://www.xiaonei.com");
System.out.println("end.");
} public static void DownLoadPages(String urlStr, String outPath) {
int chByte = 0; URL url = null; HttpURLConnection httpConn = null; InputStream in = null; FileOutputStream out = null; try {
String post = "email=" + URLEncoder.encode("e-mail", "UTF-8")
+ "&password=" + "password";
url = new URL(urlStr); httpConn = (HttpURLConnection) url.openConnection(); //setInstanceFollowRedirects can then be used to set if
//redirects should be followed or not and this should be used before the
//connection is established (via getInputStream, getResponseCode, and other
//methods that result in the connection being established). httpConn.setFollowRedirects(false); //inorder to disable the redirects
httpConn.setInstanceFollowRedirects(false); httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestProperty("User-Agent",
"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); //ok now, we can post it
PrintStream send = new PrintStream(httpConn.getOutputStream());
send.print(post);
send.close();
URL newURL = new URL(httpConn.getHeaderField("Location"));
System.out.println("the URL has move to "
+ httpConn.getHeaderField("Location"));
httpConn.disconnect(); // OK, now we are ready to get the cookies out of the URLConnection
String cookies = getCookies(httpConn);
System.out.println(cookies);
httpConn = (HttpURLConnection) newURL.openConnection();
httpConn.setRequestProperty("User-Agent",
"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpConn.setRequestProperty("Cookie", cookies); httpConn.setDoInput(true);
in = httpConn.getInputStream();
out = new FileOutputStream(new File(outPath)); chByte = in.read();
while (chByte != -1) {
out.write(chByte);
//System.out.println(chByte);
chByte = in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
} public static String getCookies(HttpURLConnection conn) {
StringBuffer cookies = new StringBuffer();
String headName;
for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {
StringTokenizer st = new StringTokenizer(headName, "; ");
while (st.hasMoreTokens()) {
cookies.append(st.nextToken() + "; ");
}
}
return cookies.toString();
}
}
java HttpURLConnection 登录网站 完整代码的更多相关文章
- java mail实现Email的发送,完整代码
java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...
- 单点登录SSO:可一键运行的完整代码
单点登录方案不同于一个普通站点,它的部署比较繁琐:涉及到好几个站点,要改host.安装证书.配置HTTPS. 看到的不少这方面示例都是基于HTTP的,不认同这种简化: 1. 它体现不出混合HTTP/H ...
- QQ登录集成到自己网站php代码(转载)
我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...
- 用python代码模拟登录网站
方法一:直接使用已知的cookie访问 特点: 简单,但需要先在浏览器登录 具体步骤: 1.用浏览器登录,获取浏览器里的cookie字符串 先使用浏览器登录.再打开开发者工具,转到network选项卡 ...
- 完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络]
完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络] 地址:http://blog.csdn.net/qq_35101189/article/details/53729720?ref=m ...
- C#DES加密,JavaDES解密,另转C#和Java实现Des完整代码
C#DES加密,JavaDES解密,另转C#和Java实现Des完整代码 转载 2014年06月17日 17:36:09 标签: DES / C#DES / JavaDES / C#和Java交叉DE ...
- Java执行shell脚本并返回结果两种方法的完整代码
Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...
- Java 扫描微信公众号二维码,关注并自动登录网站
https://blog.csdn.net/qq_42851002/article/details/81327770 场景:用户扫描微信公众号的二维码,关注后自动登录网站,若已关注则直接登录. 逻辑: ...
- CAS单点登录(SSO)完整教程
转:http://blog.csdn.net/frinder/article/details/7969925 CAS单点登录(SSO)完整教程(2012-02-01更新) 一.教程说明 前言 教程目的 ...
随机推荐
- [RxJS 6] The Retry RxJs Error Handling Strategy
When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...
- POJ 3344 & HDU 2414 Chessboard Dance(模拟)
题目链接: PKU:http://poj.org/problem? id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Descrip ...
- 创建虚拟机中的nova-scheduler 调度配置
一个Openstack 系统中通常包括多个计算节点 root@controller:/etc/nova# nova hypervisor-list +----+-------------------- ...
- 使用excel进行数据挖掘(4)---- 突出显示异常值
使用excel进行数据挖掘(4)---- 突出显示异常值 在配置环境后.能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/a ...
- Android HAL模块实现
1. HAL介绍 Android的HAL(Hardware Abstract Layer硬件抽象层)是为了保护一些硬件提供商的知识产权而提出的.是为了避开linux的GPL束缚. 思路是把控制硬件的动 ...
- ibatis 一对多查询
<typeAlias alias="businessScopeItem" type="com.sdfrdj.vo.BusinessScopeItem"/& ...
- mysql数据库字符编码修改
mysql数据库字符编码修改 修改数据库的字符集mysql>use mydb mysql>alter database mydb character set utf8; 创建数据库指定数据 ...
- 我在SharePoint行业的从业经历(二)
本文是我的SharePoint从业经历的第二篇,第一篇请參考 我在SharePoint行业的从业经历(一) 做完那个项目之后.对SharePoint 2003有了一些认识. 可是后来几年我就没在 ...
- oc35--自定义构造方法
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...
- git fetch批处理,遍历一个文件夹下的所有子目录,执行git fetch --all
echo off for /d %%i in (*) do ( echo %%i cd %%i git fetch --all cd .. ) 判断子目录是否有.git文件夹 echo off for ...