后院自动发帖代码(C#版)
RT,网上泛滥的论坛自动发帖机器人,俺也比划着写了一个
vs2005 c#.net2.0
具体程序界面还没有完成,爱生活,爱后院作类已经差不多写好
code:
///有几个地方没有弄好,一是cookie的处理,需要将Domain的.spark.cjlu.edu.cn改为spark.cjlu.edu.cn,不知为何;二是discuz的formhash机制还没有搞清楚
///另:有python/perl高手的话可以指导我一下,我在FC5,PYTHON2.5下老是没有搞好那个cookiejar,无法模拟发帖
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 usr,string pass)
{
string Return = null;
this.LoginUrl = url;
// may be I should add a functin for create string
string loginstr = "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
{
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.Length);
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 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 = false;
// 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();
return 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;
}
}
vs2005 c#.net2.0
具体程序界面还没有完成,爱生活,爱后院作类已经差不多写好
code:
///有几个地方没有弄好,一是cookie的处理,需要将Domain的.spark.cjlu.edu.cn改为spark.cjlu.edu.cn,不知为何;二是discuz的formhash机制还没有搞清楚
///另:有python/perl高手的话可以指导我一下,我在FC5,PYTHON2.5下老是没有搞好那个cookiejar,无法模拟发帖
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 usr,string pass)
{
string Return = null;
this.LoginUrl = url;
// may be I should add a functin for create string
string loginstr = "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
{
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.Length);
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 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 = false;
// 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();
return 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;
}
}