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

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

    }
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值