|
| 1 | +package com.tgb.ccl.http; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import org.apache.http.Header; |
| 11 | +import org.apache.http.client.CookieStore; |
| 12 | +import org.apache.http.client.protocol.HttpClientContext; |
| 13 | +import org.apache.http.impl.client.BasicCookieStore; |
| 14 | + |
| 15 | +import com.tgb.ccl.http.common.HttpHeader; |
| 16 | +import com.tgb.ccl.http.exception.HttpProcessException; |
| 17 | +import com.tgb.ccl.http.httpclient.HttpClientUtil; |
| 18 | + |
| 19 | +/** |
| 20 | + * 测试携带cookie的操作 |
| 21 | + * |
| 22 | + * @author arron |
| 23 | + * @date 2016年1月7日 上午10:09:53 |
| 24 | + * @version 1.0 |
| 25 | + */ |
| 26 | +public class TestCookie { |
| 27 | + |
| 28 | + public static void main(String[] args) throws HttpProcessException { |
| 29 | + //登录地址 |
| 30 | + String loginUrl = "https://passport.csdn.net/account/login"; |
| 31 | + //C币查询 |
| 32 | + String scoreUrl = "http://my.csdn.net/my/score"; |
| 33 | + |
| 34 | + HttpClientContext context = new HttpClientContext(); |
| 35 | + CookieStore cookieStore = new BasicCookieStore(); |
| 36 | + context.setCookieStore(cookieStore); |
| 37 | + //获取参数 |
| 38 | + String loginform = HttpClientUtil.send(loginUrl, context); |
| 39 | +// System.out.println(loginform); |
| 40 | + System.out.println("获取登录所需参数"); |
| 41 | + String lt = regex("\"lt\" value=\"([^\"]*)\"", loginform)[0]; |
| 42 | + String execution = regex("\"execution\" value=\"([^\"]*)\"", loginform)[0]; |
| 43 | + String _eventId = regex("\"_eventId\" value=\"([^\"]*)\"", loginform)[0]; |
| 44 | + |
| 45 | + //组装参数 |
| 46 | + Map<String, Object> map = new HashMap<String, Object>(); |
| 47 | + map.put("username", "用户名"); |
| 48 | + map.put("password", "密码"); |
| 49 | + map.put("lt", lt); |
| 50 | + map.put("execution", execution); |
| 51 | + map.put("_eventId", _eventId); |
| 52 | + |
| 53 | + //发送登录请求 |
| 54 | + String result = HttpClientUtil.send(loginUrl, map, context); |
| 55 | +// System.out.println(result); |
| 56 | + if(result.contains("帐号登录")){//如果有帐号登录,则说明未登录成功 |
| 57 | + String errmsg = regex("\"error-message\">([^<]*)<", result)[0]; |
| 58 | + System.err.println("登录失败:"+errmsg); |
| 59 | + return; |
| 60 | + } |
| 61 | + System.out.println("----登录成功----"); |
| 62 | + |
| 63 | +// //打印参数,可以看到cookie里已经有值了。 |
| 64 | +// cookieStore = context.getCookieStore(); |
| 65 | +// for (Cookie cookie : cookieStore.getCookies()) { |
| 66 | +// System.out.println(cookie.getName()+"--"+cookie.getValue()); |
| 67 | +// } |
| 68 | + |
| 69 | + //访问积分管理页面 |
| 70 | + Header[] headers = HttpHeader.custom().userAgent("User-Agent: Mozilla/5.0").build(); |
| 71 | + result = HttpClientUtil.send(scoreUrl, headers, context); |
| 72 | + //获取C币 |
| 73 | + String score = regex("\"last-img\"><span>([^<]*)<", result)[0]; |
| 74 | + System.out.println("您当前有C币:"+score); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * 通过正则表达式获取内容 |
| 81 | + * |
| 82 | + * @param regex 正则表达式 |
| 83 | + * @param from 原字符串 |
| 84 | + * @return |
| 85 | + */ |
| 86 | + public static String[] regex(String regex, String from){ |
| 87 | + Pattern pattern = Pattern.compile(regex); |
| 88 | + Matcher matcher = pattern.matcher(from); |
| 89 | + List<String> results = new ArrayList<String>(); |
| 90 | + while(matcher.find()){ |
| 91 | + for (int i = 0; i < matcher.groupCount(); i++) { |
| 92 | + results.add(matcher.group(i+1)); |
| 93 | + } |
| 94 | + } |
| 95 | + return results.toArray(new String[]{}); |
| 96 | + } |
| 97 | +} |
0 commit comments