需要自己设置 bduss,bduss 设置方法:
登录百度后在首页按 F12,在调试窗口左上角 Filter 中填 www.baidu.com
在 Headers 选项卡中 Request Headers 中找到 BDUSS= 复制其后的字符 赋值给代码中的 bduss 即可
所需依赖:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.8</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency>
或者直接用 commons-httpclient 的依赖,commons-httpclient 包含 httpclient 与 httpcore 包里部分内容
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
如果你使用普通导 jar 包的 Java 项目,则需要的 jar 包除了以上之外还需要的 jar 有:commons-logging、commons-codec,可自行在 Maven 仓库下载
代码:
/** * Author: https://cway.top * Date: 2019/7/9 * Time: 10:35 * Description: 百度签到 */ class BaiduSigner { /** * 百度签到 */ public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(200); // 设置最大连接数 cm.setDefaultMaxPerRoute(10); // 设置每个主机的并发数 // String bduss=args[0]; String bduss = ""; // 设置自己的 bduss 字符 或者从 args 获取 JSONObject tbs = (JSONObject) JSON.parse(getTbs(bduss, cm)); if (!tbs.getString("is_login").equals("1")) { System.out.println(" 可能 cookie 失效咯 "); return; } System.out.println(" 登录成功 >>>>>"); String tbstr = tbs.getString("tbs"); // System.out.println(vipSign(tbstr, bduss, cm)); //vip 一键签到 一般不需要 JSONObject jo = (JSONObject) JSON.parse(getForums(bduss, cm)); JSONObject forum_list = jo.getJSONObject("forum_list"); JSONArray jsonArray = forum_list.getJSONArray("non-gconforum"); System.out.println(" 获取列表成功 >>>>>"); int n = 0; int count = 0; for (Object o : jsonArray) { System.out.println("========== 第 " + ++n + " 个 =========="); JSONObject forum = (JSONObject) JSON.toJSON(o); String id = forum.getString("id"); String name = forum.getString("name"); System.out.println(" 正在签到 " + name + ":"); String signResult = signForums(name, id, tbstr, bduss, cm); JSONObject result = (JSONObject) JSON.parse(signResult); if (signResult.contains("error_code") && signResult.contains("error_msg")) { System.out.println(result.getString("error_msg")); continue; } JSONObject user_info = result.getJSONObject("user_info"); System.out.println(" 签到成功! 经验 +" + user_info.getString("sign_bonus_point") + ", 今天第 " + user_info.getString("user_sign_rank") + " 个签到 "); count++; } System.out.println(" 共签到成功 " + count + " 个贴吧 "); } /** * 获取贴吧状态码 tbs */ public static String getTbs(String bduss, PoolingHttpClientConnectionManager cm) throws Exception { String header = "Cookie=" + "BDUSS=" + bduss; return postClient("http://tieba.baidu.com/dc/common/tbs", header, "", cm).getResponseStr(); } /** * 获取所有关注贴吧 */ public static String getForums(String bduss, PoolingHttpClientConnectionManager cm) throws Exception { String header = new StringBuilder().append("Cookie=BDUSS=").append(bduss).append("&") .append("Content-Type=application/x-www-form-urlencoded&Charset=UTF-8&net=3&") .append("User-Agent=bdtb for Android 8.4.0.1&Connection=Keep-Alive&Accept-Encoding=gzip&") .append("Host=c.tieba.baidu.com").toString(); String md5Hex = DigestUtils.md5Hex(new StringBuilder().append("BDUSS=").append(bduss) .append("_client_version=8.1.0.4page_no=1page_size=100tiebaclient!!!") .toString()); String body = new StringBuilder().append("BDUSS=").append(bduss).append("&") .append("_client_version=8.1.0.4&page_no=1&page_size=100&sign=") .append(md5Hex).toString(); return postClient("http://c.tieba.baidu.com/c/f/forum/like", header, body, cm).getResponseStr(); } /** * 签到 */ public static String signForums(String name, String id, String tbs, String bduss, PoolingHttpClientConnectionManager cm) throws Exception { String header = new StringBuilder().append("Cookie=BDUSS=").append(bduss).append("&") .append("Content-Type=application/x-www-form-urlencoded&Charset=UTF-8&net=3&") .append("User-Agent=bdtb for Android 8.4.0.1&Connection=Keep-Alive&Accept-Encoding=gzip&") .append("Host=c.tieba.baidu.com").toString(); String md5Hex = DigestUtils.md5Hex(new StringBuilder().append("BDUSS=").append(bduss) .append("fid=" + id + "kw=" + name + "tbs=" + tbs + "tiebaclient!!!") .toString()); String body = new StringBuilder().append("BDUSS=").append(bduss) .append("&fid=").append(id).append("&kw=").append(name) .append("&sign=").append(md5Hex).append("&tbs=").append(tbs).toString(); return postClient("http://c.tieba.baidu.com/c/c/forum/sign", header, body, cm).getResponseStr(); } /** * VIP 签到 */ public static String vipSign(String tbs, String bduss, PoolingHttpClientConnectionManager cm) throws Exception { String firefoxHeader = "Content-Type: application/x-www-form-urlencoded\n" + "Charset: UTF-8\n" + "net: 3\n" + "User-Agent: bdtb for Android 8.4.0.1\n" + "Connection: Keep-Alive\n" + "Accept-Encoding: gzip\n" + "Host: c.tieba.baidu.com"; firefoxHeader = firefoxHeader.replaceAll(": ", "=").replaceAll("\n", "&"); System.out.println(" 火狐:" + firefoxHeader); String header = new StringBuilder().append("Cookie=BDUSS=").append(bduss).append("&") .append(firefoxHeader).toString(); String body = new StringBuilder().append("ie=utf-8&tbs=").append(tbs).toString(); return postClient("http://tieba.baidu.com/tbmall/onekeySignin1", header, body, cm).getResponseStr(); } /** * post 封装 * * @param url 请求 URL * @param header 请求头 * @param body 请求体 * @param cm cm 连接池 * @return 返回 Response 与页面字符 * @throws Exception */ public static ResponseVo postClient(String url, String header, String body, PoolingHttpClientConnectionManager cm) throws Exception { CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build(); HttpPost httpPost = new HttpPost(url); if (StringUtils.isNotBlank(header)) { for (Map.Entry<String, String> entry : covertParam(header)) { httpPost.addHeader(entry.getKey(), entry.getValue()); } } List<NameValuePair> pairs = new ArrayList<>(); if (StringUtils.isNotBlank(body)) { for (Map.Entry<String, String> entry : covertParam(body)) { pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, "utf-8"); httpPost.setEntity(formEntity); String content = null; CloseableHttpResponse response = null; ResponseVo responseVo = null; try { // 使用 HttpClient 发起请求 response = client.execute(httpPost); // 判断响应状态码是否为 200 if (response.getStatusLine().getStatusCode() == 200) { // 如果为 200 表示请求成功,获取返回数据 content = EntityUtils.toString(response.getEntity(), "UTF-8"); responseVo = new ResponseVo().setResponse(response).setResponseStr(content); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 if (response == null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return responseVo; } /** * 校验、转换参数 */ public static Set<Map.Entry<String, String>> covertParam(String params) throws Exception { String[] paramses; if (params.contains("=") && params.contains("&")) { paramses = params.split("&"); } else if (params.contains("=") && !params.contains("&")) { paramses = new String[1]; paramses[0] = params; } else { throw new Exception(" 参数请确保都为键值对形式 "); } Map<String, String> map = new HashMap<>(); for (String s : paramses) { String[] ss = s.split("="); map.put(ss[0], s.substring(ss[0].length() + 1)); } return map.entrySet(); } } /** * 响应结果实体类 */ class ResponseVo { private String responseStr; private CloseableHttpResponse response; public ResponseVo(String responseStr, CloseableHttpResponse response) { this.responseStr = responseStr; this.response = response; } public ResponseVo() { } public String getResponseStr() { return responseStr; } public ResponseVo setResponseStr(String responseStr) { this.responseStr = responseStr; return this; } public CloseableHttpResponse getResponse() { return response; } public ResponseVo setResponse(CloseableHttpResponse response) { this.response = response; return this; } }
以下是非 Maven 版,即导 jar 包的版本,可以用 cmd 启动,试用期请编辑 cmd 文件中内容,另附源码:
链接:https://pan.baidu.com/s/1mqWEYzYrxBYVqxrq-wON3g
提取码:fq91
复制这段内容后打开百度网盘手机 App,操作更方便哦