The example of this article for everyone to share the Java simulation HTTP GET POST request, Campus BBS automatic reply function, for your reference, the specific contents are as follows
Design ideas
Find the collection of post links, the last number of changes, you can get different posts
Prevent the post will be deleted again, to determine whether the post exists
To traverse this collection, post requests for replies to each link
Key points
Note:
- Replies require user Login information
- One is the use of cookies
- The other is to simulate a login
- This paper uses the former
Code
The code is relatively simple, the note is to find their own cookies, assigned to string Yourcookeie can be run directly
The main thing is to judge the existence of a post, this is a GET request, and then send a reply with post, the reply information in mapdata.put ("message", "Friendship help Top") in the hard code for "friendship Help Top", you can modify
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 < 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) chrom
e/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 audited")) {System.out.println ("id= + ID +" POST exists or has been deleted!)
");
return false;
(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 to help the top");
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) chrom
e/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 () = = InputStream Inputstr = con.getinputsTream ();
String info = new String (Streamtool.read (INPUTSTR), "UTF-8"); System.out.println ("in id= + ID +" Successful posting!)
");
try {thread.sleep (20 * 1000);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
(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 O
Utstr = 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 ();
}
}
Effect chart
The above is the entire content of this article, I hope to help you learn, to achieve a post automatic reply.