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;
}
}