C# 模拟网页登录[转]

 

经过实测,此代码可用。

 

using  System.Net;




public   class  HttpRec

{

        
public   string  _Url  =   " http://login.xiaonei.com/Login.do " ;

        
private   string  _strErr;

        
private  CookieContainer _CookieContainer  =   new  CookieContainer();


        
public   string  GetCode()

        {

                HttpWebRequest rqq 
=  (HttpWebRequest)HttpWebRequest.Create(_Url);

                rqq.Method 
=   " Get " ;

                rqq.KeepAlive 
=   true

                
if  (rqq.CookieContainer  ==   null )

                {

                        rqq.CookieContainer 
=  _CookieContainer;

                }

                HttpWebResponse rpp 
=  (HttpWebResponse)rqq.GetResponse();

                
return   "" ;

        }


        
public   string  LoginWeb( string  PostData)

        {

                
string  str  =   string .Empty;

                HttpWebRequest req 
=  (HttpWebRequest)HttpWebRequest.Create(_Url); // 创建req 

                req.Accept 
=   " */* " // 接受任意文件   

                req.UserAgent 
=   "  Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727) " //  模拟使用IE在浏览 


                req.UserAgent 
=   " Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16 " ;


                req.KeepAlive 
=   true ;             


                req.CookieContainer 
=  _CookieContainer;


                
if  ((PostData  !=   null   &  PostData.Length  >   0 ||  _Url.Contains( " ? " ))

                {

                        req.Method 
=   " POST " ;

                        req.ContentType 
=   " application/x-www-form-urlencoded " ;


                        
byte [] b  =  Encoding.Default.GetBytes(PostData);

                        req.ContentLength 
=  b.Length;

                        req.AutomaticDecompression 
=  DecompressionMethods.GZip;

                        System.IO.Stream sw 
=   null ;

                        
try

                        {

                                sw 
=  req.GetRequestStream();

                                sw.Write(b, 
0 , b.Length);

                        }

                        
catch  (System.Exception ex)

                        {

                                
this ._strErr  =  ex.Message;

                        }

                        
finally

                        {

                                
if  (sw  !=   null ) { sw.Close(); }

                        }

                }

                HttpWebResponse rep 
=   null ;

                System.IO.StreamReader sr 
=   null ;

                
try

                {

                        req.Method 
=   " GET " ;

                        rep 
=  (HttpWebResponse)req.GetResponse(); 

                        sr 
=   new  System.IO.StreamReader(rep.GetResponseStream(), Encoding.UTF8);

                        str 
=  sr.ReadToEnd();

                        
if  (sr  !=   null )

                        {

                                sr.Close();

                        }


                }

                
catch  (Exception e)

                { MessageBox.Show(e.Message); }


                
return  str;

        }

}

// 实例应用

string  strId  =   " XXXXXXXXXX " ; // 用户名

string  strPassword  =   " ******** " ; // 密码

string  strsubmit  =   " YES " ;

string  postData  =   " email= "   +  strId;

postData 
+=  ( " &password= "   +  strPassword);

postData 
+=  ( " &Accept= "   +  strsubmit);

HttpRec.HttpRec HttpRec 
=   new  HttpRec.HttpRec();

HttpRec.GetCode();

string  content  =  HttpRec.LoginWeb(postData);

textBox1.Text 
=  content;

HttpRec._Url 
=   " http://tycoon.xiaonei.com/AjaxTycoon.do?action=get&select_type=1 " ;

content 
=  HttpRec.LoginWeb( "" );

textBox1.Text 
=  content;

转载于:https://www.cnblogs.com/zhaoming/archive/2009/06/18/1505443.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值