Automatic Login to discuz, post code (Part)
Last Update:2018-12-07
Source: Internet
Author: User
Class robot {// attributes // cookies private cookiecollection ckcollection = NULL; // request and response private httpwebrequest sparkrequest = NULL; private httpwebresponse sparkresponse = NULL; // some URL private string loginurl = NULL; private string replyurl = NULL; // constructer public robot () {ckcollection = new cookiecollection ();} // logining Public String login (string URL, string u Sr, string pass) {string return = NULL; this. loginurl = URL; // may be I shoshould Add a functin for create string loginstr = "formhash = 3bd8bc0a & Referer = index. PHP & loginmode = & styleid = & cookietime = 2592000 & loginfield = username & username = "+ USR; loginstr + =" & Password = "+ pass; loginstr + = "& questionid = 0 & Answer = & loginsubmit = submit"; loginstr = encodepost (loginstr); byte [] replybyte = encoding. utf8.getb Ytes (loginstr); try {cookiecontainer sparkc = new cookiecontainer (); sparkrequest = (httpwebrequest) webrequest. create (URL); sparkrequest. cookiecontainer = sparkc; sparkrequest. contenttype = "application/X-WWW-form-urlencoded"; sparkrequest. method = "Post"; sparkrequest. contentlength = replybyte. length; stream newstream = sparkrequest. getrequeststream (); newstream. write (replybyte, 0, replybyte. len Streams); newstream. close (); sparkresponse = (httpwebresponse) sparkrequest. getresponse (); stream datastream = sparkresponse. getresponsestream (); streamreader reader = new streamreader (datastream, encoding. getencoding ("gb2312"); return = reader. readtoend (); // check cookie foreach (cookie temp in sparkresponse. cookies) {If (temp. domain! = "Spark.cjlu.edu.cn") temp. domain = "spark.cjlu.edu.cn";} ckcollection = sparkresponse. cookies;} catch {return NULL;} return;} // overload/* Public bool login (string USR, string pass) {;} * // reply ...... Public String reply (string URL, string formhash, String title, string content) {sparkrequest = (httpwebrequest) webrequest. create ("http://spark.cjlu.edu.cn/bbs/" + URL); sparkrequest. contenttype = "application/X-WWW-form-urlencoded"; sparkrequest. method = "Post"; // sparkrequest. referer = "http://spark.cjlu.edu.cn/bbs/index.php"; sparkrequest. keepalive = true; sparkrequest. allowwritestreambuffering = fa LSE; // set cookie cookiecontainer cookiecon = new cookiecontainer (); sparkrequest. cookiecontainer = cookiecon; sparkrequest. cookiecontainer. add (ckcollection); // get post value string reply = encodepost ("formhash =" + formhash + "& subject = & usesig = 1 & message =" + content ); byte [] replybyte = encoding. utf8.getbytes (reply); sparkrequest. contentlength = replybyte. length; stream newstream = sparkrequest. Getrequeststream (); newstream. write (replybyte, 0, replybyte. length); newstream. close (); // get response sparkresponse = (httpwebresponse) sparkrequest. getresponse (); stream datastream = sparkresponse. getresponsestream (); streamreader reader = new streamreader (datastream, encoding. getencoding ("gb2312"); string TT = reader. readtoend (); reader. close (); datastream. close (); sparkresponse. close (); retur N tt;} // encode the post string private string encodepost (string input) {string output = NULL; char [] reserved = {'? ',' = ',' & '}; If (input! = NULL) {int I = 0, J; while (I <input. length) {J = input. indexofany (reserved, I); If (j =-1) {output = output + httputility. urlencode (input. substring (I, input. length-I), system. text. encoding. getencoding ("gb2312"); break;} string TT = httputility. urlencode (input. substring (I, j-I), system. text. encoding. getencoding ("gb2312"); Output + = tt; Output + = input. substring (J, 1); I = J + 1;} return output;} else return NULL ;}}