Automatic Login to discuz, post code (Part)

Source: Internet
Author: User
Tags set cookie
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 ;}}

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.