Java模擬HTTP Get Post請求 輕松實(shí)現(xiàn)校園BBS自動回帖
本文實(shí)例為大家分享了Java模擬HTTP Get Post請求,校園BBS自動回帖功能,供大家參考,具體內(nèi)容如下
設(shè)計(jì)思路
找到帖子鏈接的集合,最后面數(shù)字變化, 就可以得到不同的帖子
防止帖子發(fā)表會又被刪了的情況, 進(jìn)行判斷帖子是否存在
遍歷這個(gè)集合, 對每個(gè)鏈接做回帖的POST請求
重難點(diǎn)
Note:
- 回帖需要用戶登錄信息
- 一種是利用Cookie
- 另一種是進(jìn)行模擬登錄
- 本文采用前者
代碼
代碼比較簡單,注意事項(xiàng)是找到自己的Cookie,賦給String yourCookeie就可以直接運(yùn)行
主要就是判斷帖子存不存在,這是一個(gè)get請求,然后用post發(fā)送一個(gè)回帖,回帖信息在mapData.put(“message”, “友情幫頂了”)中 硬編碼為”友情幫頂了”,你可以修改
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.LinkedHashMap; import java.util.Map; public class Inter { private static final String baseRefer = "http://rs.xidian.edu.cn/forum.php?mod=viewthread&tid="; private static final String yourCookeie = "Q8qA_2132_saltkey=g1NJjJ3O; Q8qA_2132_lastvisit=1438243699; Q8qA_2132_lastcheckfeed=256730%7C1438252008; Q8qA_2132_auth=e11aEhhXpLgTYpfDK72YJZEgJHL1v70cUXXDtJ71VbU2dyuH%2BQHw3pGOJhsFxfjbVgNsvyfG1v%2BQlD0lt8kg6J%2B40W0; Q8qA_2132_st_t=256730%7C1438571068%7C51f8a322985e44f65ff1143329e6779a; Q8qA_2132_forum_lastvisit=D_106_1438571068; Q8qA_2132_myrepeat_rr=R0; Q8qA_2132_ulastactivity=d7degfMAwG5AGHshmT%2BwCq1L91znQpEa57p%2F0Vt7VHdC8DrOuGTT; Q8qA_2132_home_diymode=1; tjpctrl=1438781938176; Q8qA_2132_visitedfid=72D551D215D110D13D142D22D91D217D548; Q8qA_2132_st_p=256730%7C1438781224%7C7a73ef608dc3caf733308d63639b3bd0; Q8qA_2132_viewid=tid_773850; Q8qA_2132_smile=10D1; Q8qA_2132_sid=ZnfqQN; Q8qA_2132_lastact=1438781403%09forum.php%09ajax"; public static void main(String[] args) { int startId = 774210; // you need change for (int i = 0; i < 100; i++) { postMessage(startId); startId++; } } public static boolean isExist(int id) { String tmpPath = baseRefer + id; URL url; try { url = new URL(tmpPath); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.addRequestProperty("Content-Type", "text/html; charset=UTF-8"); con.addRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"); con.addRequestProperty("Referer", "http://t.dianping.com/register"); con.setRequestMethod("GET"); if (con.getResponseCode() == 200) { InputStream inputStr = con.getInputStream(); String info = new String(StreamTool.read(inputStr), "UTF-8"); if (info.contains("抱歉,指定的主題不存在或已被刪除或正在被審核")) { System.out.println("id=" + id + "帖子存在或已被刪除!"); return false; } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } public static void postMessage(int id) { if (!isExist(id)) { return; } String tmpPath = baseRefer + id; StringBuilder path = new StringBuilder(tmpPath); Map<String, String> mapData = new LinkedHashMap<String, String>(); mapData.put("mod", "post"); mapData.put("action", "reply"); mapData.put("replysubmit", "yes"); mapData.put("infloat", "yes"); mapData.put("handlekey", "fastpost"); mapData.put("inajax", "1"); mapData.put("message", "友情幫頂了"); mapData.put("formhash", "86ec5d81"); try { for (Map.Entry<String, String> mapEnt : mapData.entrySet()) { path.append("&"); path.append(mapEnt.getKey() + "="); path.append(URLEncoder.encode(mapEnt.getValue(), "UTF-8")); } URL url = new URL(path.toString()); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Length", String.valueOf(path.length())); con.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"); con.setRequestProperty("Cookie", yourCookeie); con.setDoOutput(true); OutputStream outStr = con.getOutputStream(); outStr.write(path.toString().getBytes()); if (con.getResponseCode() == 200) { InputStream inputStr = con.getInputStream(); String info = new String(StreamTool.read(inputStr), "UTF-8"); System.out.println("在id=" + id + "成功發(fā)帖!"); try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class StreamTool { public static byte[] read(InputStream inputStr) throws Exception { ByteArrayOutputStream outStr = new ByteArrayOutputStream(); // TODO Auto-generated method stub byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStr.read(buffer)) != -1) { outStr.write(buffer, 0, len); } inputStr.close(); return outStr.toByteArray(); } }
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,實(shí)現(xiàn)帖子自動回復(fù)。
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之校園一卡通系統(tǒng)的實(shí)現(xiàn)
- Java實(shí)戰(zhàn)項(xiàng)目之校園跑腿管理系統(tǒng)的實(shí)現(xiàn)
- Java?實(shí)戰(zhàn)范例之校園二手市場系統(tǒng)的實(shí)現(xiàn)
- Java 實(shí)戰(zhàn)練手項(xiàng)目之校園超市管理系統(tǒng)的實(shí)現(xiàn)流程
- Java 實(shí)戰(zhàn)項(xiàng)目錘煉之校園宿舍管理系統(tǒng)的實(shí)現(xiàn)流程
- Java實(shí)現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼
- JavaWeb開發(fā)基于ssm的校園服務(wù)系統(tǒng)(實(shí)例詳解)
- Java基于Dijkstra算法實(shí)現(xiàn)校園導(dǎo)游程序
相關(guān)文章
Java中實(shí)現(xiàn)文件預(yù)覽的功能(實(shí)例代碼)
大家都知道word,Excel,PPT實(shí)現(xiàn)在線預(yù)覽常用的方式就是先轉(zhuǎn)換成pdf,然后在進(jìn)行預(yù)覽,下面給大家介紹Java中如何實(shí)現(xiàn)文件預(yù)覽的功能,需要的朋友可以參考下2023-05-05Java實(shí)現(xiàn)多線程中的靜態(tài)代理模式
靜態(tài)代理屬于設(shè)計(jì)模式中的代理模式。這篇文章主要介紹了Java實(shí)現(xiàn)多線程中的靜態(tài)代理模式,詳細(xì)的介紹了靜態(tài)代理的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05使用fastjson中的JSONPath處理json數(shù)據(jù)的方法
這篇文章主要介紹了使用fastjson中的JSONPath處理json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04字節(jié)二面SpringBoot可以同時(shí)處理多少請求
這篇文章主要為大家介紹了字節(jié)二面之SpringBoot可以同時(shí)處理多少請求面試分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java中使用HashMap時(shí)指定初始化容量性能解析
這篇文章主要為大家介紹了Java中使用HashMap時(shí)指定初始化容量性能解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫
本篇文章主要介紹了spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12Java中ThreadLocal使用原理及Synchronized區(qū)別
ThreadLocal叫做線程變量,本文詳細(xì)的介紹了ThreadLocal使用原理及Synchronized區(qū)別,有需要的朋友可以參考一下,希望對你有所幫助。2023-05-05如何實(shí)用Java實(shí)現(xiàn)合并、拆分PDF文檔
這篇文章主要介紹了如何實(shí)用Java實(shí)現(xiàn)合并、拆分PDF文檔,處理PDF文檔時(shí),這樣的好處是對文檔的存儲、管理很方便。下面將通過Java程序代碼介紹具體的PDF合并、拆分的方法,需要的朋友可以參考下2019-07-07