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

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

 

None.gif class  Robot
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// attributes
InBlock.gif        // cookies
InBlock.gif
        private CookieCollection CkCollection = null;
InBlock.gif        
// request and response
InBlock.gif
        private HttpWebRequest SparkRequest = null;
InBlock.gif        
private HttpWebResponse SparkResponse = null;
InBlock.gif        
// some url
InBlock.gif
        private string LoginUrl = null;
InBlock.gif        
private string ReplyUrl = null;
InBlock.gif
InBlock.gif        
// constructer
InBlock.gif
        public Robot()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CkCollection 
= new CookieCollection();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// logining
InBlock.gif
        public string Login(string url, string usr,string pass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string Return = null;
InBlock.gif            
this.LoginUrl = url;
InBlock.gif            
// may be I should add a functin for create string
InBlock.gif
            string loginstr = "formhash=3bd8bc0a&referer=index.php&loginmode=&styleid=&cookietime=2592000&loginfield=username&username=" + usr;
InBlock.gif            loginstr 
+= "&password=" + pass;
InBlock.gif            loginstr 
+= "&questionid=0&answer=&loginsubmit=提 交";
InBlock.gif            loginstr 
= EncodePost(loginstr);
InBlock.gif            
byte[] replybyte = Encoding.UTF8.GetBytes(loginstr);
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CookieContainer sparkc 
= new CookieContainer();
InBlock.gif                SparkRequest 
= (HttpWebRequest)WebRequest.Create(url);
InBlock.gif                SparkRequest.CookieContainer 
= sparkc;
InBlock.gif                SparkRequest.ContentType 
= "application/x-www-form-urlencoded";
InBlock.gif                SparkRequest.Method 
= "POST";
InBlock.gif
InBlock.gif                SparkRequest.ContentLength 
= replybyte.Length;
InBlock.gif                Stream newStream 
= SparkRequest.GetRequestStream();
InBlock.gif                newStream.Write(replybyte, 
0, replybyte.Length);
InBlock.gif                newStream.Close();
InBlock.gif
InBlock.gif                SparkResponse 
= (HttpWebResponse)SparkRequest.GetResponse();
InBlock.gif                Stream dataStream 
= SparkResponse.GetResponseStream();
InBlock.gif                StreamReader reader 
= new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));
InBlock.gif                Return 
= reader.ReadToEnd();
InBlock.gif
InBlock.gif                
// check cookie
InBlock.gif
                foreach (Cookie temp in SparkResponse.Cookies)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (temp.Domain != "spark.cjlu.edu.cn")
InBlock.gif                        temp.Domain 
= "spark.cjlu.edu.cn";
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                CkCollection 
= SparkResponse.Cookies;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// overload
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//*
InBlock.gif        public bool Login(string usr, string pass)
InBlock.gif        {
InBlock.gif            ;
ExpandedSubBlockEnd.gif        }
*/

InBlock.gif
InBlock.gif        
// reply……
InBlock.gif
        public string Reply(string url,string formhash,string title,string content)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SparkRequest 
= (HttpWebRequest)WebRequest.Create("http://spark.cjlu.edu.cn/bbs/"+url);
InBlock.gif            SparkRequest.ContentType 
= "application/x-www-form-urlencoded";
InBlock.gif            SparkRequest.Method 
= "POST";
InBlock.gif            
//SparkRequest.Referer = "http://spark.cjlu.edu.cn/bbs/index.php";
InBlock.gif
            SparkRequest.KeepAlive = true;
InBlock.gif            SparkRequest.AllowWriteStreamBuffering 
= false;
InBlock.gif
InBlock.gif            
// set cookie
InBlock.gif
            CookieContainer cookieCon = new CookieContainer();
InBlock.gif            SparkRequest.CookieContainer 
= cookieCon;
InBlock.gif            SparkRequest.CookieContainer.Add(CkCollection);
InBlock.gif
InBlock.gif            
// get post value
InBlock.gif
            string reply = EncodePost("formhash=" + formhash + "&subject=&usesig=1&message=" + content);
InBlock.gif            
byte[] replybyte = Encoding.UTF8.GetBytes(reply);
InBlock.gif            SparkRequest.ContentLength 
= replybyte.Length;
InBlock.gif            Stream newStream 
= SparkRequest.GetRequestStream();
InBlock.gif            newStream.Write(replybyte, 
0, replybyte.Length);
InBlock.gif            newStream.Close();
InBlock.gif
InBlock.gif            
// get response
InBlock.gif
            SparkResponse = (HttpWebResponse)SparkRequest.GetResponse();
InBlock.gif            Stream dataStream 
= SparkResponse.GetResponseStream();
InBlock.gif            StreamReader reader 
= new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));
InBlock.gif            
string tt = reader.ReadToEnd();
InBlock.gif
InBlock.gif            reader.Close();
InBlock.gif            dataStream.Close();
InBlock.gif            SparkResponse.Close();
InBlock.gif
InBlock.gif            
return tt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// encode the post string
InBlock.gif
        private string EncodePost(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string output = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif            Char[] reserved 
= dot.gif'?''=''&' };
InBlock.gif            
if (input != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = 0, j;
InBlock.gif                
while (i < input.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    j 
= input.IndexOfAny(reserved, i);
InBlock.gif                    
if (j == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        output 
= output + HttpUtility.UrlEncode(input.Substring(i, input.Length - i), System.Text.Encoding.GetEncoding("gb2312"));
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

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

InBlock.gif                
return output;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
 
None.gif

转载于:https://www.cnblogs.com/melorain/archive/2007/02/01/637345.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值