- 浏览: 128779 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
qindongliang1922:
不错!
linux sort 命令 -
eve:
这个还真没注意,8错
protected访问权限又理解了一下 -
imp860124:
学习了。
struts ActionForward跳转添加参数
static String cookie; static String url ="http://www.iteye.com/login"; static String name="xxx"; static String password ="xxx"; public static void main(String[] args) throws Exception{ //keep http connection execute in same httpclient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse resp = httpClient.execute(get); cookie = resp.getFirstHeader("Set-Cookie").getValue(); HttpEntity e = resp.getEntity(); String r =EntityUtils.toString(e,"gbk"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name", name)); params.add(new BasicNameValuePair("password", password)); params.add(new BasicNameValuePair("authenticity_token", getToken(r))); postMethod(params, httpClient); } static String getToken(String html){ //解析下返回内容,获取登陆必须的token参数 Document doc = Jsoup.parse(html); Element ef = doc.getElementById("login_form"); Elements einputs = ef.getElementsByTag("input"); for(Element e : einputs){ if(e.attr("name").equals("authenticity_token")){ return e.attr("value"); } } return ""; } public static void postMethod(List<NameValuePair> params, HttpClient httpClient) throws Exception, IOException{ HttpPost post = new HttpPost(url); /* * 没用到 * post.setHeader("Host", "www.iteye.com"); post.setHeader("Origin", "http://www.iteye.com"); post.setHeader("Referer", "http://www.iteye.com/login"); post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"); post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*;q=0.8"); post.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6"); post.setHeader("Cache-Control", "max-age=0"); post.setHeader("Connection", "keep-alive"); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setHeader("Cookie", cookie);*/ post.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(post); int status = response.getStatusLine().getStatusCode(); if(status==HttpStatus.SC_MOVED_PERMANENTLY || status==HttpStatus.SC_MOVED_TEMPORARILY){//redirect post.releaseConnection(); String location = response.getFirstHeader("location").getValue(); HttpPost redirectPost = new HttpPost(location); HttpResponse resp = httpClient.execute(redirectPost); HttpEntity entity = resp.getEntity(); System.out.println(resp.getFirstHeader("Set-Cookie").getValue()); String result =EntityUtils.toString(entity,"gbk"); //登陆后返回首页内容,若想进一步获取内容,可以继续解析返回内容 System.out.println(result); redirectPost.releaseConnection(); } }
发表评论
-
java 工程中静态常量类问题
2015-05-27 10:42 991在搭建java工程时,一般都会建有一个常量类Constants ... -
java计算排列结果内容
2014-06-16 11:06 580递归方法 /** * 递归计算 * @param ... -
java 序列化
2014-03-18 17:41 878以有序的字节数组形式描述java对象, 包括对象的描述信息(类 ... -
java 调用 oracle 存储过程
2014-02-08 14:45 586private static String url =&qu ... -
log4j 配置
2014-01-22 17:49 653log4j日志配置 1.log4j.rootLogger ... -
tomcat 使用log4j记录分割日志
2014-01-16 10:51 5555tomcat日志记录默认使用java.util.logging ... -
java 正则
2013-10-29 16:47 720\d 数字0-9 \D 非数字[^0-9] \s 空白字符[ ... -
javabean 嵌套属性赋值
2013-04-02 09:33 1613import java.beans.PropertyD ... -
eclipse 取消汉化
2013-03-06 11:36 1817方法一:在eclipse.ini文件中添加启动参数 -Duse ... -
java 进度条上传
2013-02-28 10:00 866上传ajax方式,可用juery.ajaxupload插件 ... -
java 调用 webservice
2013-01-07 11:14 2191只想调用远处webservice,不构建自己的,看了一些资料都 ... -
java nio SocketChannel 服务器端与多客户端 信息交互(聊天功能)
2013-01-05 15:24 5037服务器端: import java.io.IOExcept ... -
java nio buffer
2012-12-31 17:54 1068buffer是固定数量的数据的容器,可以被填充和读取。 一个 ... -
java Integer
2012-09-06 14:33 0java Integer比较 public stat ... -
Apache Commons DbUtils
2011-12-06 11:11 1494dbutils jar下载地址http://labs.renr ... -
solr服务器的搭建与简单使用
2011-08-17 11:30 0solr安装: 1.下载(v1.4.1) ht ... -
protected访问权限又理解了一下
2011-06-30 14:46 1621看java的克隆时遇到问题 不能直接调用Object类的clo ... -
lucence hello world
2011-04-27 12:24 1084/* * lucence version 3.0.3 ... -
局域网流量监控原理(java)
2011-04-25 11:11 2317使用jpcap开源java包。(http://netresea ... -
apache commons beanutils
2011-02-22 16:16 1547官网下载jar包http://commons.apache.o ...
相关推荐
NULL 博文链接:https://lxzqz.iteye.com/blog/2051318
NULL 博文链接:https://shaozhen.iteye.com/blog/950472
NULL 博文链接:https://laravel.iteye.com/blog/807039
NULL 博文链接:https://piranha.iteye.com/blog/2119924
使用HttpClient登录网易邮箱 博文链接:https://bps.iteye.com/blog/136231
NULL 博文链接:https://shaozhen.iteye.com/blog/946680
NULL 博文链接:https://eric-wyf.iteye.com/blog/1179729
NULL 博文链接:https://hualom.iteye.com/blog/1119713
NULL 博文链接:https://fatalove.iteye.com/blog/814563
NULL 博文链接:https://jilongliang.iteye.com/blog/1275107
用httpclient开发的在线自动抢订火车票系统 此代码为转载 原文链接:http://www.iteye.com/topic/1119632
NULL 博文链接:https://gary0416.iteye.com/blog/900498
NULL 博文链接:https://kukuqiu.iteye.com/blog/343019
NULL 博文链接:https://mr--fang.iteye.com/blog/1562669
NULL 博文链接:https://skymr.iteye.com/blog/1911341
NULL 博文链接:https://javasam.iteye.com/blog/2117845
NULL 博文链接:https://zhouxianglh.iteye.com/blog/832696
NULL 博文链接:https://wenkaixuan.iteye.com/blog/2109590