首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

winfrm模拟自动登录错误,

2011-12-24 
winfrm模拟自动登录错误,高手请进。每次登陆http://www2.baidu.com/太麻烦,做了个模拟自动登陆的跳转到另外

winfrm模拟自动登录错误,高手请进。
每次登陆http://www2.baidu.com/太麻烦,做了个模拟自动登陆的跳转到另外一页http://www2.baidu.com/query/query_list.php并取html源码,但是有错误,错误在程序中已标出。那位大侠帮帮忙改改。
protected   static   string   cookieheader;
private   void   Form1_Load(object   sender,   EventArgs   e)
{
        string   param   =   "Action=&entered_login=luo&entered_password=123&entered_imagecode=ohtm "
        byte[]   bs   =   Encoding.UTF8.GetBytes(param);
        HttpWebRequest   req   =   (HttpWebRequest)HttpWebRequest.Create( "http://www2.baidu.com/ ");
        req.Method   =   "POST ";
        req.AllowAutoRedirect   =   true;
        req.ContentType   =   "application/x-www-form-urlencoded ";
        req.ContentLength   =   bs.Length;

        CookieContainer   cookieCon   =   new   CookieContainer();
        req.CookieContainer   =   cookieCon;

        using   (Stream   reqStream   =   req.GetRequestStream())
        {
                //req.Timeout   =   3000;
                reqStream.Write(bs,   0,   bs.Length);

                HttpWebResponse   res   =   res   =   (HttpWebResponse)req.GetResponse();
                cookieheader   =   req.CookieContainer.GetCookieHeader(new   Uri( "http://www2.baidu.com/ "));
                HttpContext.Current.Application.Lock();//错误为未将对象引用到实列
                HttpContext.Current.Application[ "cookieheader "]   =   cookieheader;
                HttpContext.Current.Application.UnLock();
        }
        using   (WebResponse   wr   =   req.GetResponse())
        {
                StreamReader   reader   =   new   StreamReader(wr.GetResponseStream(),   Encoding.Default);
                richTextBox1.Text   =   reader.ReadToEnd();
        }
        getpage();//用已有的cookie身份跳转的另外一页
}

public   static   string   getPage()  
{  
        HttpWebResponse   res   =   null;  
        string   strResult   =   " ";  
        try  
        {  
                string   param   =   "Action=&entered_login=luo&entered_password=123&entered_imagecode=ohtm "
                string   url= "http://www2.baidu.com/query/query_list.php ";
                HttpWebRequest   req   =   (HttpWebRequest)WebRequest.Create(url);  
                req.Method   =   "POST ";  


                req.KeepAlive   =   true;  
                req.ContentType   =   "application/x-www-form-urlencoded ";  
                CookieContainer   cookieCon   =   new   CookieContainer();  
                req.CookieContainer   =   cookieCon;  
                req.CookieContainer.SetCookies(new   Uri(url),cookieheader);  
                byte[]   SomeBytes   =   null;  
                SomeBytes   =   Encoding.UTF8.GetBytes(param);  
                req.ContentLength   =   SomeBytes.Length;  
                Stream   newStream   =   req.GetRequestStream();  
                newStream.Write(SomeBytes,   0,   SomeBytes.Length);  
                newStream.Close();  
                res   =   (HttpWebResponse)req.GetResponse();  
                Stream   ReceiveStream   =   res.GetResponseStream();  
                Encoding   encode   =   System.Text.Encoding.GetEncoding( "utf-8 ");  
                StreamReader   sr   =   new   StreamReader(   ReceiveStream,   encode   );  
                richTextBox1.Text   =   reader.ReadToEnd();
        }
}

[解决办法]
代码太长,没看
自动登录不就是代码写入一个用户名一个密码,然后调用一下登录按钮的那个事件?
也就是人点鼠标改成直接调用而已,没啥区别
[解决办法]
代码太长,没看
自动登录不就是代码写入一个用户名一个密码,然后调用一下登录按钮的那个事件?
也就是人点鼠标改成直接调用而已,没啥区别

--------------------------
ls的真搞笑...
[解决办法]
HttpContext.Current.Application.Lock();//错误为未将对象引用到实列
HttpContext.Current.Application[ "cookieheader "] = cookieheader;
HttpContext.Current.Application.UnLock();

改成
res.Cookies = res.CookieContainer.GetCookies(req.RequestUri);

热点排行