httpContext使用
2016-01-07 15:03
531 查看
public class LoginCsdn { /** * HttpClientContext这个参数了,给它设置了cookiestore,那么会在每次请求时将cookie带入请求中。 * 或者也可以在header中手动设置cookie参数,也是可以做到的。 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //登录地址 String loginUrl = "https://passport.csdn.net/account/login"; HttpClientContext context = new HttpClientContext(); CookieStore cookieStore = new BasicCookieStore(); context.setCookieStore(cookieStore); //获取参数 String loginform = send(loginUrl, null, context); // System.out.println(loginform); System.out.println("获取登录所需参数"); String lt = regex("\"lt\" value=\"([^\"]*)\"", loginform)[0]; String execution = regex("\"execution\" value=\"([^\"]*)\"", loginform)[0]; String _eventId = regex("\"_eventId\" value=\"([^\"]*)\"", loginform)[0]; //组装参数 Map<String, String> map = new HashMap<>(); map.put("username", ""); map.put("password", ""); map.put("lt", lt); map.put("execution", execution); map.put("_eventId", _eventId); //发送登录请求 String result = send(loginUrl, map, context); // System.out.println(result); if(result.contains("帐号登录")){//如果有帐号登录,则说明未登录成功 String errmsg = regex("\"error-message\">([^<]*)<", result)[0]; System.err.println("登录失败:"+errmsg); return; } System.out.println("----登录成功----"); //打印参数,可以看到cookie里已经有值了。 cookieStore = context.getCookieStore(); for (Cookie cookie : cookieStore.getCookies()) { System.out.println(cookie.getName()+"--"+cookie.getValue()); } } /** * 通过正则表达式获取内容 * * @param regex 正则表达式 * @param from 原字符串 * @return */ public static String[] regex(String regex, String from){ Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(from); List<String> results = new ArrayList<String>(); while(matcher.find()){ for (int i = 0; i < matcher.groupCount(); i++) { results.add(matcher.group(i+1)); } } return results.toArray(new String[]{}); } public static String send(String url, Map<String, String> params, HttpClientContext context) throws Exception{ CloseableHttpClient httpClient = HttpClientPool.getHttpClient(); HttpPost httpPost = new HttpPost(url); if (params!=null && !params.isEmpty()) { List<NameValuePair> list = new ArrayList<NameValuePair>(); for (Entry<String, String> param : params.entrySet()) { list.add(new BasicNameValuePair(param.getKey(), param.getValue())); } //构建url加密实体,并以utf-8方式进行加密; UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8); httpPost.setEntity(entity); } CloseableHttpResponse response = httpClient.execute(httpPost, context); return EntityUtils.toString(response.getEntity()); } }
相关文章推荐
- linux网络编程实用命令
- 常用LINUX系统网络分析命令
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- PHP实现基于Swoole简单的HTTP服务器
- SSL 网络安全协议 HTTPS
- HttpUploader6-queue版本更新说明
- 【TCP/IP】C语言实现Ping小程序
- 新线程网络请求,回调方法在主线程中进行
- 网络通信协议笔记
- 西安鹏程张建宁老师主讲网络安全
- 基于C#的socket编程的TCP异步实现
- COMODO反间谍软件BOClean在线下载
- 页面自动跳转与http302、html refresh以及js跳转之间的关系
- iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息
- Java web开发(3)——Http
- StringHttpMessageConverter 乱码问题
- Spring Http Basic(基本)和Digest(摘要)验证
- 使用HttpFileServer自建下载服务器
- 令牌桶算法和漏桶算法
- 对TCP连接被重置解决方案的探究