圓梦

激情燃燒的歲月
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

ASP.Net随想_实现WEB表单外部提交

Posted on 2007-04-27 12:15  szw104  阅读(490)  评论(0编辑  收藏  举报

ASP.Net随想_实现WEB表单外部提交

[日期:2007-04-11] 来源:http://www.cnblogs.com/Roping/archive/2007/04/07/7  作者:Roping [字体: ]
 上篇文章ASP.Net随想_了解下原始的东西HTTP简单的分析了aspx页面请求提交的HTTP数据包,并且说到我会写个自动注册机或者发帖的工具的,呵呵,工作忙得一笔,实在是抽不出身!今天休息,就接着上篇写下去吧!
    
上次我们监控到的HTTP数据包有两个属性很重要:

      POST :后面的东西,就是说这个请求处理的页面(URL

      Cokie:保持HTTP请求连续的标志,

      还有一个也很重要:Referer,因为好多网站判断这个标志是否来自什么地方请求防止恶意注册,呵呵,知道该怎么做了吗?模拟你的请求吧!准备写个自动注册的东西,开始工作。寻找目标http://foxconnbbs.com/index.asp,呵呵,富士康的,我喜欢!随便找个帖子:http://foxconnbbs.com/dispbbs.asp?boardID=3&ID=3749&page=1注意URL参数的写法,这个和你自动生成所有的回帖有关,我就不在写这个Demo只是真对这个帖子用Dot.Net实现外部提交!打开ieHTTPHeaders,分析下提交的数据格式:


呵呵,直到处理提交的URL: savepost.asp?action=sre&method=fastreply&BoardID=3,呵呵,别忘记前面加http://foxconnbbs.com/。为什么?晕到!Cookie也知道了吧!呵呵,提交的数据就是这一部分了。结下来就是如何模拟这个请求了,要想模拟这个请求,必须得出cookie的值,呵呵,如何得到这个Cookie
哪?还得模拟登陆获得!看下登陆界面:



点击登录后注意提交数据,
呵呵,自己分析吧!



Ok!
开始干活,自动登录获得Cookie的方法如下:

public string ShamLogin(string url, string usr,string pwd)
        
{
            
string Return = null
;
            
this.LoginUrl =
 url;            
            
string loginstr = "username="+usr+"&password="+pwd+"&CookieDate=0&submit=%B5%C7%C2%BC"
;
            

                 
            loginstr 
=
 EncodePost(loginstr);
            
byte[] replybyte =
 Encoding.UTF8.GetBytes(loginstr);
            
            
try

            
{
                CookieContainer testCC 
= new
 CookieContainer();
                TestRequest 
=
 (HttpWebRequest)WebRequest.Create(url);
                TestRequest.CookieContainer 
=
 testCC;
                TestRequest.ContentType 
= "application/x-www-form-urlencoded"
;
                TestRequest.Method 
= "POST"
;

                TestRequest.ContentLength 
=
 replybyte.Length;
                Stream newStream 
=
 TestRequest.GetRequestStream();
                newStream.Write(replybyte, 
0
, replybyte.Length);
                newStream.Close();

                TestResponse 
=
 (HttpWebResponse)TestRequest.GetResponse();
                Stream dataStream 
=
 TestResponse.GetResponseStream();
                StreamReader reader 
= new StreamReader(dataStream, Encoding.GetEncoding("gb2312"
));
                Return 
=
 reader.ReadToEnd();

                
// check cookie

                foreach (Cookie temp in TestResponse.Cookies)
                
{
                    
if (temp.Domain != "foxconnbbs.com"
)
                        temp.Domain 
= "foxconnbbs.com"
;
                }


                CkCollection 
= TestResponse.Cookies;
            }

            
catch
            
{
                
return null
;
            }

            
return Return;
        }


 回复的代码如下:

public string Reply(string url,string formhash,string title,string content)
        
{
            
//
post.php?action=reply&fid=84&tid=45444&extra=page%3D1&replysubmit=yes HTTP/1.1
            
//formhash=dcf4e770&subject=%B6%F1%C6%F8&message=eqw

            TestRequest = (HttpWebRequest)WebRequest.Create("http://foxconnbbs.com/savepost.asp?action=sre&method=fastreply&BoardID=3");
            TestRequest.ContentType 
= "application/x-www-form-urlencoded"
;
            TestRequest.Method 
= "POST"
;
            TestRequest.Referer 
= "http://foxconnbbs.com/dispbbs.asp?boardid=3&id=3711&star=1"
;
            TestRequest.KeepAlive 
= true
;
            TestRequest.AllowWriteStreamBuffering 
= false
;

            
// set cookie

            CookieContainer cookieCon = new CookieContainer();
            TestRequest.CookieContainer 
=
 cookieCon;
            TestRequest.CookieContainer.Add(CkCollection);

            
//
 get post value
            
//formhash=dcf4e770&subject=%B6%F1%C6%F8&message=eqw

            string reply = EncodePost("Body=%A3%BF%A3%BF%A3%BF&followup=18085&RootID=3711&star=1&TotalUseTable=dv_bbs1&UserName=roping&Expression=face1.gif&signflag=1&Submit=OK%21%B7%A2%B1%ED%BB%D8%B8%B4");
            
byte[] replybyte =
 Encoding.UTF8.GetBytes(reply);
            TestRequest.ContentLength 
=
 replybyte.Length;
            Stream newStream 
=
 TestRequest.GetRequestStream();
            newStream.Write(replybyte, 
0
, replybyte.Length);
            newStream.Close();

            
// get response

            TestResponse = (HttpWebResponse)TestRequest.GetResponse();
            Stream dataStream 
=
 TestResponse.GetResponseStream();
            StreamReader reader 
= new StreamReader(dataStream, Encoding.GetEncoding("gb2312"
));
            
string tt =
 reader.ReadToEnd();

            reader.Close();
            dataStream.Close();
            TestResponse.Close();

            
return
 tt;
        }