<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】 ? ? ?最近更新了一下[HttpClientUtil工具類](https://github.com/Arronlong/httpclientUtil)代碼,主要是添加了一個參數HttpContext,這個是用來干嘛的呢?其實是用來保存和傳遞Cookie所需要的。因為我們有很多時候都需要登錄,然后才能請求一些想要的數據。而在這以前使用HttpClientUtil工具類,還不能辦到。現在更新了以后,終于可以了。 ? ? ?先說一下思路:本次的demo,就是獲取csdn中的c幣,要想獲取c幣,必須先登錄。而每次登錄需要5個參數。其中2個必不可少的參數是用戶名和密碼,其他的3個參數,是需要從登錄頁面獲取的。在第一次請求登錄頁面,只要設置了CookieStore,就可以自動獲取cookie了,然后從返回的html源碼中獲取參數,再組裝添加用戶名密碼,然后第二次登錄,如果返回的html源碼中有“帳號登錄”這幾個字,就說明登錄失敗了。否則登錄成功。可以打印一下cookie(已注釋)。然后再訪問c幣查詢的頁面,就可以從返回的html源碼中解析到c幣的值了。登錄時需要注意的是:直接提交用戶名密碼或者第二次登錄不攜帶context參數,是不能登錄成功的。 ? ? ?具體代碼如下: ~~~ public static void main(String[] args) throws HttpProcessException { //登錄地址 String loginUrl = "https://passport.csdn.net/account/login"; //C幣查詢 String scoreUrl = "http://my.csdn.net/my/score"; HttpClientContext context = new HttpClientContext(); CookieStore cookieStore = new BasicCookieStore(); context.setCookieStore(cookieStore); //獲取參數 String loginform = HttpClientUtil.send(loginUrl, 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, Object> map = new HashMap<String, Object>(); map.put("username", "用戶名"); map.put("password", "密碼"); map.put("lt", lt); map.put("execution", execution); map.put("_eventId", _eventId); //發送登錄請求 String result = HttpClientUtil.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()); // } //訪問積分管理頁面 Header[] headers = HttpHeader.custom().userAgent("Mozilla/5.0").build(); result = HttpClientUtil.send(scoreUrl, headers, context); //獲取C幣 String score = regex("\"last-img\"><span>([^<]*)<", result)[0]; System.out.println("您當前有C幣:"+score); } ~~~ ? ? ?從html源碼中解析參數和c幣值所用到的一個方法: ~~~ /** * 通過正則表達式獲取內容 * * @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[]{}); } ~~~ ? ? ?測試結果: ![](https://box.kancloud.cn/2016-02-18_56c53cb865fff.jpg) ![](https://box.kancloud.cn/2016-02-18_56c53cb876cee.jpg) ? ? ?最重要的就是context這個參數了,給它設置了cookiestore,那么會在每次請求時將cookie帶入請求中。或者也可以在header中手動設置cookie參數,也是可以做到的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看