欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java模擬HTTP Get Post請(qǐng)求實(shí)現(xiàn)論壇自動(dòng)回帖功能

 更新時(shí)間:2016年09月05日 15:17:37   投稿:lijiao  
這篇文章主要介紹了Java模擬HTTP Get Post請(qǐng)求實(shí)現(xiàn)論壇自動(dòng)回帖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近想自動(dòng)發(fā)帖回帖,拿某論壇試驗(yàn)了一下,發(fā)現(xiàn)可行,不過(guò)后續(xù)沒(méi)有再使用,以免影響論壇正常運(yùn)行。

1、帖子鏈接的格式為
http://bbs.***.***.**/forum.php?mod=viewthread&tid=774210

最后面774210數(shù)字變化, 就可以得到不同的帖子

2、防止帖子發(fā)表會(huì)又被刪了的情況, 進(jìn)行判斷帖子是否存在

3、遞增后面的 id 數(shù)字, 對(duì)每個(gè)鏈接做回帖的 POST 請(qǐng)求

重難點(diǎn)

回帖需要用戶登錄信息
一種是利用Cookie
另一種是進(jìn)行模擬登錄

本文采用前者

判斷 url 對(duì)應(yīng)的帖子是否存在
有可能用戶發(fā)了帖子,比如 url 為 http://bbs.***.***.**/forum.php?mod=viewthread&tid=774200

后來(lái)該帖子用戶刪除了或者被管理員刪除了,雖然帖子不在了,但是該 tid=774200 還是存在的

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;
}

模擬發(fā)帖
代碼比較簡(jiǎn)單,注意事項(xiàng)是找到自己的Cookie,賦給String yourCookeie

用post發(fā)送一個(gè)回帖,回帖信息在 mapData.put("message", "友情幫頂了") 中

private static final String baseRefer = "http://bbs.**.**.**/forum.php?mod=viewthread&tid=";
private static final String yourCookeie = "Q8qA_2132_saltkey=**; Q8qA_2132_lastvisit=****3699;";
public static void main(String[] args) {
 int startId = 774210; // you need change
 for (int i = 0; i < 100; i++) {
 postMessage(startId);
 startId++;
 }
}
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();
 }
}

還有一個(gè)工具方法,將輸入流轉(zhuǎn)化為字節(jié)

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();
 }
}

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • idea中項(xiàng)目前端網(wǎng)頁(yè)圖標(biāo)不顯示的原因及解決

    idea中項(xiàng)目前端網(wǎng)頁(yè)圖標(biāo)不顯示的原因及解決

    這篇文章主要介紹了idea中項(xiàng)目前端網(wǎng)頁(yè)圖標(biāo)不顯示的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spring項(xiàng)目XML文件使用小結(jié)

    Spring項(xiàng)目XML文件使用小結(jié)

    這篇文章主要介紹了Spring項(xiàng)目XML文件使用常見介紹,主要包括項(xiàng)目pom文件,項(xiàng)目初始IOC容器及項(xiàng)目需要自動(dòng)裝配的代碼詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Java實(shí)現(xiàn)斷點(diǎn)下載功能的示例代碼

    Java實(shí)現(xiàn)斷點(diǎn)下載功能的示例代碼

    當(dāng)下載一個(gè)很大的文件時(shí),如果下載到一半暫停,如果繼續(xù)下載呢?斷點(diǎn)下載就是解決這個(gè)問(wèn)題的。本文將用Java語(yǔ)言實(shí)現(xiàn)斷點(diǎn)下載,需要的可以參考一下
    2022-05-05
  • spring?boot配置dubbo方式(properties)

    spring?boot配置dubbo方式(properties)

    這篇文章主要介紹了spring?boot配置dubbo方式(properties),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 深入理解Java中Filter的作用種類及應(yīng)用場(chǎng)景

    深入理解Java中Filter的作用種類及應(yīng)用場(chǎng)景

    Filter(過(guò)濾器)是Java Web中的一種重要組件,可以對(duì)請(qǐng)求和響應(yīng)進(jìn)行攔截處理,對(duì)數(shù)據(jù)進(jìn)行過(guò)濾和處理。Filter可以實(shí)現(xiàn)許多功能,如:鑒權(quán)、日志記錄、字符編碼轉(zhuǎn)換、數(shù)據(jù)壓縮、請(qǐng)求重定向等等
    2023-04-04
  • java web開發(fā)之servlet圖形驗(yàn)證碼功能的實(shí)現(xiàn)

    java web開發(fā)之servlet圖形驗(yàn)證碼功能的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了java web開發(fā)之servlet中圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java結(jié)合Kotlin實(shí)現(xiàn)寶寶年齡計(jì)算

    Java結(jié)合Kotlin實(shí)現(xiàn)寶寶年齡計(jì)算

    這篇文章主要為大家介紹了Java結(jié)合Kotlin實(shí)現(xiàn)寶寶年齡計(jì)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 讓Java后臺(tái)MySQL數(shù)據(jù)庫(kù)能夠支持emoji表情的方法

    讓Java后臺(tái)MySQL數(shù)據(jù)庫(kù)能夠支持emoji表情的方法

    最近開發(fā)的iOS項(xiàng)目因?yàn)樾枰脩粑谋镜拇鎯?chǔ),自然就遇到了emoji等表情符號(hào)如何被mysql DB支持的問(wèn)題。下面這篇文章主要介紹了關(guān)于讓Java后臺(tái)MySQL數(shù)據(jù)庫(kù)能夠支持emoji表情的方法,需要的朋友可以參考下。
    2017-03-03
  • Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能示例

    Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能示例

    這篇文章主要介紹了Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能,結(jié)合實(shí)例形式分析了java使用socket進(jìn)行多線程數(shù)據(jù)傳輸?shù)南嚓P(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Java雜談之合格程序員一定要會(huì)閱讀別人的源碼

    Java雜談之合格程序員一定要會(huì)閱讀別人的源碼

    閱讀別人的代碼作為開發(fā)人員是一件經(jīng)常要做的事情。一個(gè)是學(xué)習(xí)新的編程語(yǔ)言的時(shí)候通過(guò)閱讀別人的代碼是一個(gè)最好的學(xué)習(xí)方法,另外是積累編程經(jīng)驗(yàn)
    2021-09-09

最新評(píng)論