自动登录DISCUZ,发帖的代码(部分)

有点无聊的东西,不是通用的,不过RD提供了我们论坛用的discuz的hashform的计算代码,也许通用的DISCUZ灌水机器人是我这种菜鸟也能搞出来的。
关于代码,没有什么技术含量,先用一个叫Ethereal的软件抓到提交的值,然后用.NET中对应的库即可完成。下面是主要的类:

class Robot
{
/**////attributes
//cookies
privateCookieCollectionCkCollection=null;
//requestandresponse
privateHttpWebRequestSparkRequest=null;
privateHttpWebResponseSparkResponse=null;
//someurl
privatestringLoginUrl=null;
privatestringReplyUrl=null;

//constructer
publicRobot()
{
CkCollection
=newCookieCollection();
}


//logining
publicstringLogin(stringurl,stringusr,stringpass)
{
stringReturn=null;
this.LoginUrl=url;
//maybeIshouldaddafunctinforcreatestring
stringloginstr="formhash=3bd8bc0a&referer=index.php&loginmode=&styleid=&cookietime=2592000&loginfield=username&username="+usr;
loginstr
+="&password="+pass;
loginstr
+="&questionid=0&answer=&loginsubmit=提交";
loginstr
=EncodePost(loginstr);
byte[]replybyte=Encoding.UTF8.GetBytes(loginstr);

try
{
CookieContainersparkc
=newCookieContainer();
SparkRequest
=(HttpWebRequest)WebRequest.Create(url);
SparkRequest.CookieContainer
=sparkc;
SparkRequest.ContentType
="application/x-www-form-urlencoded";
SparkRequest.Method
="POST";

SparkRequest.ContentLength
=replybyte.Length;
StreamnewStream
=SparkRequest.GetRequestStream();
newStream.Write(replybyte,
0,replybyte.Length);
newStream.Close();

SparkResponse
=(HttpWebResponse)SparkRequest.GetResponse();
StreamdataStream
=SparkResponse.GetResponseStream();
StreamReaderreader
=newStreamReader(dataStream,Encoding.GetEncoding("gb2312"));
Return
=reader.ReadToEnd();

//checkcookie
foreach(CookietempinSparkResponse.Cookies)
{
if(temp.Domain!="spark.cjlu.edu.cn")
temp.Domain
="spark.cjlu.edu.cn";
}


CkCollection
=SparkResponse.Cookies;
}

catch
{
returnnull;
}

returnReturn;
}


//overload
/**//*
publicboolLogin(stringusr,stringpass)
{
;
}
*/


//reply……
publicstringReply(stringurl,stringformhash,stringtitle,stringcontent)
{
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
=false;

//setcookie
CookieContainercookieCon=newCookieContainer();
SparkRequest.CookieContainer
=cookieCon;
SparkRequest.CookieContainer.Add(CkCollection);

//getpostvalue
stringreply=EncodePost("formhash="+formhash+"&subject=&usesig=1&message="+content);
byte[]replybyte=Encoding.UTF8.GetBytes(reply);
SparkRequest.ContentLength
=replybyte.Length;
StreamnewStream
=SparkRequest.GetRequestStream();
newStream.Write(replybyte,
0,replybyte.Length);
newStream.Close();

//getresponse
SparkResponse=(HttpWebResponse)SparkRequest.GetResponse();
StreamdataStream
=SparkResponse.GetResponseStream();
StreamReaderreader
=newStreamReader(dataStream,Encoding.GetEncoding("gb2312"));
stringtt=reader.ReadToEnd();

reader.Close();
dataStream.Close();
SparkResponse.Close();

returntt;
}


//encodethepoststring
privatestringEncodePost(stringinput)
{
stringoutput=null;
Char[]reserved
={'?','=','&'};
if(input!=null)
{
inti=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;
}

stringtt=HttpUtility.UrlEncode(input.Substring(i,j-i),System.Text.Encoding.GetEncoding("gb2312"));
output
+=tt;
output
+=input.Substring(j,1);
i
=j+1;
}

returnoutput;
}

else
returnnull;
}

}

你可能感兴趣的:(discuz)