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 登录网站 完整代码的更多相关文章

  1. java mail实现Email的发送,完整代码

    java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...

  2. 单点登录SSO:可一键运行的完整代码

    单点登录方案不同于一个普通站点,它的部署比较繁琐:涉及到好几个站点,要改host.安装证书.配置HTTPS. 看到的不少这方面示例都是基于HTTP的,不认同这种简化: 1. 它体现不出混合HTTP/H ...

  3. QQ登录集成到自己网站php代码(转载)

    我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...

  4. 用python代码模拟登录网站

    方法一:直接使用已知的cookie访问 特点: 简单,但需要先在浏览器登录 具体步骤: 1.用浏览器登录,获取浏览器里的cookie字符串 先使用浏览器登录.再打开开发者工具,转到network选项卡 ...

  5. 完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络]

    完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络] 地址:http://blog.csdn.net/qq_35101189/article/details/53729720?ref=m ...

  6. 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 ...

  7. Java执行shell脚本并返回结果两种方法的完整代码

    Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...

  8. Java 扫描微信公众号二维码,关注并自动登录网站

    https://blog.csdn.net/qq_42851002/article/details/81327770 场景:用户扫描微信公众号的二维码,关注后自动登录网站,若已关注则直接登录. 逻辑: ...

  9. CAS单点登录(SSO)完整教程

    转:http://blog.csdn.net/frinder/article/details/7969925 CAS单点登录(SSO)完整教程(2012-02-01更新) 一.教程说明 前言 教程目的 ...

随机推荐

  1. 洛谷—— P2733 家的范围 Home on the Range

    https://www.luogu.org/problem/show?pid=2733 题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因 ...

  2. N天学习一个linux命令之sort

    用途 对文本内容按行排序,输出排好序后的内容到标准输出流 用法 sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F 常用选项 -b, ...

  3. <script>标签的加载解析执行

    转自原文 <script>标签的加载解析执行 看了很多网上的文章,都是大同小异.总结一下.内部原理还没有搞清楚,有机会再学习. 一.<script>标签的加载解析执行顺序 ht ...

  4. MVC.Net: jqueryval错误

    当使用Mvc.net创建Create表单后,firebug Create页面会出现404 Not Found - http://192.168.3.95:7001/bundles/jqueryval& ...

  5. Codeforces Round #306 (Div. 2) D

    D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. 【安卓笔记】ormlite入门

    ps:写这篇文章的目的是尝试下新的markdown编辑器哈哈 简单介绍 ORMLite provides a lightweight Object Relational Mapping between ...

  7. 使用golang来设计我们的Ubuntu Scope

    我们知道golang越来越被非常多的开发人员来开发应用.go语言也能够用于开发Ubuntu Scope. 在今天的教程中.我们将具体介绍怎样使用go语言来开发我们的Scope.这对于非常多的不太熟悉C ...

  8. Unix网络编程 高级IO套接字设置超时

    我们知道.对于一个套接字的读写(read/write)操作默认是堵塞的.假设当前套接字还不可读/写,那么这个操作会一直堵塞下去,这样对于一个须要高性能的server来说,是不能接受的.所以,我们能够在 ...

  9. 移动App服务端架构设计

      我从事手机app服务端开发现在已经是3个年头,自己也整理出了一套相对好用的服务架构,写出来,跟大家一起分享.如有不足,还请多指教. 一:基础流程图. 其实有一点还需要加上,就是对json的压缩和加 ...

  10. [转]RDLC 动态列

    本文转自:http://blog.csdn.net/luochengbang/article/details/9964551 很久没有写博客了,关于动态列,国内很少资料有介绍动态列的,所想写点心得给哥 ...