I recently wanted to post automatically and replies automatically. I tried it with a forum and found that it was feasible, but it was not used again in the future to avoid affecting the normal operation of the forum.
1. The format of the post link is
http://bbs.***.***.**/forum.php?mod=viewthread&tid=774210
The number changes at the end of the 774210 can be obtained by getting different posts
2. Prevent the post from being deleted again, and determine whether the post exists
3. Increment the id number afterwards, and make a POST request to reply to each link
Key points
Replying requires user login information One is to use cookies
Another way is to simulate login
This article adopts the former
Determine whether the corresponding post of url exists
It is possible that the user has posted a post, for example, the url is http://bbs.***.***.**/forum.php?mod=viewthread&tid=774200
Later, the user of the post was deleted or deleted by the administrator. Although the post is gone, the tid=774200 still exists.
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("Sorry, the specified topic does not exist or has been deleted or is being reviewed")) { System.out.println("id=" + id + "Post exists or has been deleted!"); 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(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true;}Simulate posting <br />The code is relatively simple, the precaution is to find your own cookie and assign it to String yourCookie
Send a reply using post, and the reply message is in mapData.put("message", "Friendship Help")
private static final String baseRefer = "http://bbs.**.**.**/forum.php?mod=viewthread&tid=";private static final String yourCookie = "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", "Friendship support"); 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", yourCookie); 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("submitted successfully in id=" + id + "! "); 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(); }} There is also a tool method that converts input streams into bytes
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(); }}Reproduction image:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.