Java simulated HTTP GET POST request Implementation Forum Automatic reply function _java

Source: Internet
Author: User

Recently want to automatically post replies, take a forum to test a bit, found feasible, but no further use, so as not to affect the normal operation of the forum.

1, the Post link format is
http://bbs.***.***.**/forum.php?mod=viewthread&tid=774210

The last 774210 digits change, you can get different posts

2, prevent the post will be deleted the situation, to determine whether the post exists

3, increment the following ID number, for each link to do a POST request for replies

Key points

Replies require user Login information
One is the use of cookies
The other is to simulate a login

This paper uses the former

To determine whether a URL corresponds to the existence of a post
It is possible that the user sent a post, such as a URL for http://bbs.***.***.**/forum.php?mod=viewthread&tid=774200

Later, the post user deleted or was deleted by the administrator, although the post is not, but the tid=774200 still exist

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

Analog posting
The code is relatively simple, the note is to find your own cookie, assigned to string Yourcookeie

Send a reply with post, reply information in mapdata.put ("message", "Friendship help Top")

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;//Your need change for (int i = 0; i < i++) {PO
 Stmessage (Startid);
 startid++;
 The 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) 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 () = = 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 ();
 }
 }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 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 ();
 }


Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.