[请大家讨论]后院自动发帖代码(C#版)[ZT]

jscn123789abc 2009-03-16 02:56:33
后院自动发帖代码(C#版)
引用地址
///////////////////
http://spark.cjlu.edu.cn/bbs/viewthread.php?tid=15630&extra=&page=1
///////////////////

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;
}
}
...全文
531 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jscn123789abc 2009-04-27
  • 打赏
  • 举报
回复
ding....
柳晛 2009-04-27
  • 打赏
  • 举报
回复

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;
}
}
冷月孤峰 2009-03-16
  • 打赏
  • 举报
回复
M a r k
Teng_s2000 2009-03-16
  • 打赏
  • 举报
回复
Mark
我姓区不姓区 2009-03-16
  • 打赏
  • 举报
回复
up
youlanse 2009-03-16
  • 打赏
  • 举报
回复
暂时还看不明白
sjt000 2009-03-16
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
。。。。。。。。。
wuyq11 2009-03-16
  • 打赏
  • 举报
回复
UP
l_9style 2009-03-16
  • 打赏
  • 举报
回复
标签
wuyi8808 2009-03-16
  • 打赏
  • 举报
回复
mark
relive_qiankai 2009-03-16
  • 打赏
  • 举报
回复
不会,帮顶了!
yannixinxiang 2009-03-16
  • 打赏
  • 举报
回复
up

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧