This article shares Java simulates HTTP Get Post requests and campus BBS automatic reply function for your reference. The specific content is as follows
Design ideas
Find the collection of post links and change the number at the end, and you can get different posts
Prevent the posts from being deleted again, and determine whether the post exists
Iterate through this collection and make a POST request to reply to each link
Key points
Note:
Code
The code is relatively simple. The precaution is to find your own cookie. Assign yourCookie to String yourCookie and run it directly.
The main thing is to determine whether the post exists or not. This is a get request, and then send a reply with post. The reply message is hardcoded as "Friendly Helped" in mapData.put("message", "Friendly Helped"), you can modify it
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 yourCookie = "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("Sorry, the specified topic does not exist or has been deleted or is being reviewed")) { System.out.println("id=" + id + "The 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(); } 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", "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("in id=" + id + "Successfully posted!"); 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(); }}Reproduction diagram
The above is all the content of this article. I hope it will be helpful to everyone's learning and enable automatic reply to posts.