- 浏览: 479665 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
feibabm:
楼主,想问下,我配置的这个拦截器为啥没有生效呢?import ...
spring-boot 加入拦截器Interceptor -
yhxf_ie:
项目跑步起来啊,打印完日志就 Process finished ...
spring-boot集成EasyUI和KindEditor -
yhxf_ie:
...
spring-boot集成EasyUI和KindEditor -
lihao312:
什么都不懂的孩子 写道请问一下registry.addInte ...
spring-boot 加入拦截器Interceptor -
爱you不释手:
1 、2 楼 那句的意思是加入名称为UserSecurityI ...
spring-boot 加入拦截器Interceptor
1.利用HttpClient模拟iteye登录
package com.home.action.test.http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.CookieHandler; import java.net.CookieManager; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /** * 模仿登录iTeye * @author Li */ public class LoginITEye { private String cookies; private HttpClient http = new DefaultHttpClient(); private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { String url = "http://www.iteye.com/login"; String itUrl = "http://www.iteye.com"; LoginITEye iteye = new LoginITEye(); //打开cookie CookieHandler.setDefault(new CookieManager()); //获取请求登录的页面HTML String page = iteye.getITEyePageContent(url); System.out.println(page); //设置登录参数 List<NameValuePair> params = iteye.tranParams(page, "email","password"); //开始登录 iteye.startLogin(url, params); String itEyePage = iteye.getITEyePageContent(itUrl); System.out.println("----------可以从itEyePage打印里面看到welcome欢迎***。表示登录成功.-----------"); System.out.println(itEyePage); } /** * 开始模拟登录 * @param url * @param params * @throws IOException * @throws ClientProtocolException */ private void startLogin(String url, List<NameValuePair> params) throws Exception { HttpPost request = new HttpPost(url); request.setHeader("Host", "www.iteye.com"); request.setHeader("User-Agent", USER_AGENT); request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4,zh-TW;q=0.2"); request.setHeader("Cookie", getCookies()); request.setHeader("Connection", "keep-alive"); request.setHeader("Referer", "http://www.iteye.com/login"); request.setHeader("Content-Type", "application/x-www-form-urlencoded"); request.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = http.execute(request); int statusCode = response.getStatusLine().getStatusCode(); //statusCode==200表示请求成功. System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("send paramer: "+ params); System.out.println("Response Code : " + statusCode); BufferedReader br = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(); String line = ""; while((line = br.readLine()) != null) { sb.append(line); } //System.out.println(sb.toString()); } /** * 把你的登录账号和密码设置到form里面去 * @param html * @param name 你的iTeye登录名 * @param word 你的iTeye登录密码 * @return */ private List<NameValuePair> tranParams(String html, String name, String word) { List<NameValuePair> list = new ArrayList<NameValuePair>(); //转化HTML --> document Document doc = Jsoup.parse(html); //获取ITEye登录form Element formEl = doc.getElementById("login_form"); Elements inputs = formEl.getElementsByTag("input"); //解析ITEye登录form里面的登录名和登录密码 for(Element el: inputs) { String elName = el.attr("name"); String elValue = el.attr("value"); if(elName.equals("name")) { elValue = name; }else if(elName.equals("password")) { elValue = word; } if(!elName.equals("button")) { list.add(new BasicNameValuePair(elName, elValue)); } } return list; } /** * 获取iTeye登录页面 * @param url * @return * @throws IOException * @throws ClientProtocolException */ private String getITEyePageContent(String url) throws Exception { StringBuffer result = new StringBuffer(); //设置请求信息 HttpGet request = new HttpGet(url); request.setHeader("User-Agent", USER_AGENT); request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4,zh-TW;q=0.2"); //发送请求 HttpResponse response = http.execute(request); int statusCode = response.getStatusLine().getStatusCode(); //statusCode==200表示请求成功. System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + statusCode); //读取返回内容解析成字符串 BufferedReader br = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line = ""; while((line = br.readLine()) != null) { result.append(line); } //设置cookie setCookies(response.getFirstHeader("Set-Cookie") == null ? "": response.getFirstHeader("Set-Cookie").toString()); return result.toString(); } /** * @return the cookid */ public String getCookies() { return cookies; } /** * @param cookid the cookid to set */ public void setCookies(String cookies) { this.cookies = cookies; } }
发表评论
-
Spring-boot集成Netty做websocket服务端
2017-06-21 13:47 19514spring-boot-websocket-netty-s ... -
spring-boot集成Springfox-Swagger2
2016-10-12 21:47 4809spring-boot-springfox sprin ... -
spring-boot集成Quartz
2016-10-09 20:40 13712spring-boot集成Quartz <depe ... -
spring-boot集成EasyUI和KindEditor
2016-09-25 12:12 3374spring-boot-easyui-kindeditor ... -
spring-boot集成sitemesh装饰器
2016-09-02 09:41 6827spring-boot-sitemesh, 依赖sprin ... -
spring-boot 代码生成工具
2016-08-17 10:38 4699Mybatis Auto Code Generation Ma ... -
spring-boot集成Freemarker开发
2016-07-10 11:38 210631.该demo功能 http://localhost/syst ... -
spring-boot集成shiro
2016-07-07 13:35 21221引用 项目启动后输入:http://localhost/ 该项 ... -
spring集成ActiveMQ, 消息提供者
2016-06-15 13:47 19461.spring集成ActiveMQ,依赖包 <de ... -
记录springmvc+ueditor1.4.3上传附件问题
2015-08-27 14:42 9175百度富文本编辑器地址:http://ueditor.baidu ... -
mvn jetty:run内存溢出解决
2014-12-08 12:22 1918maven内存溢出 apache-maven-3.1.0\b ... -
POI写入Excel下拉框[Select选项]
2014-08-14 09:45 14130记录部分方法, 操作POI POI version &l ... -
MD5,salt,SHA,PBKDF2加密
2014-06-11 23:11 15261package com.lance.utils; i ... -
设置java的JTextPane字体加粗
2014-05-16 10:14 2695package manage; import jav ... -
程序出错后,程序员给测试人员的20条高频回复
2014-02-18 22:12 792程序出错后 20. “That’ ... -
java字符全角转半角
2013-08-16 00:36 1455说明: 只为记录生活中的点点滴滴, 看到ITeye发 ... -
利用apache的poi上传excel
2013-07-22 11:34 20111.做个简单的导入excel功能, 只为记录简单demo, ... -
利用spring security控制同一个用户只能一次登陆
2013-07-18 14:04 96331.描述 最近在网上看了很多关于一个用户同时只能登陆一次问 ... -
获取System基本配置信息
2013-06-27 16:57 11381.获取基本信息 package com.foo.test ... -
java反射简单记录
2013-06-08 11:17 17801. Java 反射API的第一个主要作用是获取程序在运行 ...
相关推荐
NULL 博文链接:https://javaeedevelop.iteye.com/blog/1687630
使用HttpClient登录网易邮箱 博文链接:https://bps.iteye.com/blog/136231
NULL 博文链接:https://enefry.iteye.com/blog/986651
这是我在iteye网站的技术博客
NULL 博文链接:https://shaozhen.iteye.com/blog/946680
NULL 博文链接:https://heritrix.iteye.com/blog/1144961
NULL 博文链接:https://username2.iteye.com/blog/2004550
NULL 博文链接:https://shaozhen.iteye.com/blog/950472
NULL 博文链接:https://zhouxianglh.iteye.com/blog/832696
NULL 博文链接:https://itshu.iteye.com/blog/1754672
NULL 博文链接:https://jbeduhai.iteye.com/blog/1587228
NULL 博文链接:https://zrzking.iteye.com/blog/1946063
最新的ITeye月刊,覆盖各个It的行业最新信息,了解最新的咨询。
NULL 博文链接:https://piranha.iteye.com/blog/2119924
NULL 博文链接:https://jiangtie.iteye.com/blog/1003878
NULL 博文链接:https://fatalove.iteye.com/blog/814563
NULL 博文链接:https://eric-wyf.iteye.com/blog/1179729