C#网页 自动登录 提交POST信息 方法

以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:

           HtmlElement ClickBtn =null;
           if (e.Url.ToString().ToLower().IndexOf("http://sandou.cnblogs.com/") > 0)   //登陆页面
            {
                HtmlDocument doc = webBrowser1.Document;
                for (int i = 0; i < doc.All.Count ; i++)
                {
                    if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                    {
                        switch (doc.All[i].Name)
                        {
                            case "userCtl":
                                doc.All[i].InnerText = "user01";
                                break;
                            case "passCt1":
                                doc.All[i].InnerText = "mypass";
                                break;
                            case "B1":
                                ClickBtn = doc.All[i]; //提交按钮
                                break;
                        }
                    }
                }
                ClickBtn.InvokeMember("Click");   //执行按扭操作
            }

2、WebClient封装了HTTP的一些类,操作简单,相较于webBrowser,特点是可以自设代理,缺点是对COOKIE的控制
WebClient的运行全在后台,并且提供了异步操作的能力,这样很方便并发多个任务,然后等待结果的返回,再逐个处理。多任务异步调用的代码如下:

private void StartLoop(int ProxyNum)
        {
           WebClient [] wcArray = new WebClient[ProxyNum]; //初始化
             for (int idArray = 0; idArray< ProxyNum;idArray++)
            {
                 wcArray[idArray] = new WebClient();
                wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
                wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
                try
                {
                  
                    wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
                    wcArray[idArray].OpenReadAsync(new Uri("http://sandou.cnblogs.com/")); //打开WEB;
                    proxy = null;
                }
                catch
                {
                }
            }
        }

        private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
        {
                if (e.Error == null)
                {
                            string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //取返回信息
                             ..
                              String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
                             ((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                            ((WebClient)sender).Headers.Add("Accept-Language", "zh-cn");
                            ((WebClient)sender).Headers.Add("Cookie", cookie);

                            string postData = ""
                            byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成二进制数组
                           ((WebClient)sender).UploadDataAsync(new Uri("http://sandou.cnblogs.com/"), "POST", byteArray);
                }
         }

        private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
        {
                 if (e.Error == null)
                {
                    string returnMessage = Encoding.Default.GetString(e.Result);
                  
                }
       }


3、HttpWebRequest较为低层,能实现的功能较多,Cookie操作也很简单:


       private bool PostWebRequest()      
        {
                   CookieContainer cc = new CookieContainer();
                    string pos tData = "user=" + strUser + "&pass=" + strPsd;
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化

                    HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(http://sandou.cnblogs.com/));
                    webRequest2.CookieContainer = cc;
                    webRequest2.Method = "POST";
                    webRequest2.ContentType = "application/x-www-form-urlencoded";
                    webRequest2.ContentLength = byteArray.Length;
                    Stream newStream = webRequest2.GetRequestStream();
                    // Send the data.
                    newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                    newStream.Close();

                    HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
                    StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
                    string text2 = sr2.ReadToEnd();
                
        }  

HttpWebRequest 实现, 这个是从网上COPY 的!我以前用相关的代码登录到WWW.ASP.NET上,并且成功post,可惜代码不知道放什么地方了。

HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站)
可以使用 Visual Sniffer(百度搜索) 来捕捉提交的数据信息:
1. 访问你需要站外提交的页面,比如 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx
2. 填写好需要的资料,比如用户名和密码,
3. 打开 Visual Sniffer, 点“开始拦截”
4. 在访问的页面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”
6. 在 Visual Sniffer 的左侧栏的加号中依次点开,右边是它拦截到的内容:


POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, **
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879
以上为拦截内容,其中提交数据的参数部分(程序中的:strArgs)如:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU
0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu
1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDN
UserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232

        protected static string cookieHeader;
        private void Page_Load(object sender, System.EventArgs e)
        {
            string strReContent = string.Empty;
            //登录
            strReContent = PostLogin("http://www.mystand.com.cn/login/submit.jsp提交的页面","提交的参 数:userid=hgj0000&password=06045369","引用地址:http://www.mystand.com.cn /");
            //asp.net登录传递的参数需注意  
            //strReContent = PostLogin("47bDxcZTs%2BPjs%2BOzs%2BOz4%2BOz6aX2dtqkJTK%2BKbNPsjd7Op%2Fl26Iw%3D%3D&txtUserName=hxf&txtPassword=hxf0000&btnEnter=%E7%99%BB%E5%BD%95","http://www.mystand.com.cn/login.aspx">http://www.mystand.com.cn/login.aspx","__VIEWSTATE=dDwtNjkzMjUyNDczO3Q8O2w8aTwzPjs%2BO2w8dDxwPHA8bDxUZXh0Oz47bDxcZTs%2BPjs%2BOzs%2BOz4%2BOz6aX2dtqkJTK%2BKbNPsjd7Op%2Fl26Iw%3D%3D&txtUserName=hxf&txtPassword=hxf0000&btnEnter=%E7%99%BB%E5%BD%95","http://www.mystand.com.cn/login.aspx");
            //获取页面
            strReContent = GetPage("http://www.mystand.com.cn/company/getdata.jsp?code=","引用地址:http://www.mystand.com.cn/");
            //strReContent = GetPage("http://www.mystand.com.cn/Modules/index.aspx","http://www.mystand.com.cn/login.aspx");
            //可以对获得的内容进行处理:strReContent
        }

        /// <summary>
        /// 功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie
        /// </summary>
        /// <param name="strURL">登录数据提交的页面地址</param>
        /// <param name="strArgs">用户登录数据</param>
        /// <param name="strReferer">引用地址</param>
        /// <returns>可以返回页面内容或不返回</returns>
        public static string PostLogin(string strURL,string strArgs,string strReferer)
        {
            string strResult = "";
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
            myHttpWebRequest.AllowAutoRedirect = true;
            myHttpWebRequest.KeepAlive = true;
            myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */// <summary>
        /// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容
        /// </summary>
        /// <param name="strURL">获取网站的某页面的地址</param>
        /// <param name="strReferer">引用的地址</param>
        /// <returns>返回页面内容</returns>
        public static string GetPage(string strURL,string strReferer)
        {
            string strResult = "";
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
            myHttpWebRequest.ContentType = "text/html";
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.Referer = strReferer;
            myHttpWebRequest.Headers.Add("cookie:"+ cookieHeader);

            HttpWebResponse response = null;
            System.IO.StreamReader sr = null;
            response = (HttpWebResponse)myHttpWebRequest.GetResponse();
            sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));    //    //utf-8
            strResult = sr.ReadToEnd();
            return strResult;
        }

技术应用——网页自动登录(提交Post内容)的用途很多,如验证身份、程序升级、网络投票等,以下是用C#实现的方法.
==================================================================================================================
1.在WebBrowser中获取Cookie

CookieContainer myCookieContainer = new CookieContainer();

string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
    string[] cookieNameValue = str.Split('=');
    Cookie ck = new Cookie(cookieNameValue[0].Trim ().ToString(), cookieNameValue[1].Trim ().ToString());
    ck.Domain = "www.google.com";
    myCookieContainer.Add(ck);
}

WebClient设置cookie!
WebClient wc = new WebClient();
wc.Headers.Add("Cookie", "PHPSESSID=" + cookie + ";");
// 注意,这里是Cookie,不是Set-Cookie
byte[] re = wc.UploadData(Global.RootPath + "test.php", new byte[0]);
System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding();
string str = converter.GetString(re);


2. 在WebBrowser中设置Cookie
public partial class WebBrowserControl : Form
{
        private String url;

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

        public WebBrowserControl(String path)
        {
            this.url = path;
            InitializeComponent();

            // set cookie
            InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID);

            // navigate
            webBrowser.Navigate(url);
        }       
}

3.将WebBrowser的cookie信息传给HttpWebRequest

先建一个"CookieContainer" 把WebBrowser中的Cookie保存在里面

//在WebBrowser中登录cookie保存在WebBrowser.Document.Cookie中    
CookieContainer myCookieContainer = new CookieContainer();

//String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');

foreach (string str in cookstr)
{
    string[] cookieNameValue = str.Split('=');
    Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
    ck.Domain = "www.abc.com";//必须写对
    myCookieContainer.Add(ck);
}

HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
hreq.Method = "POST";
hreq.ContentType = "application/x-www-form-urlencoded";
        
//自己创建的CookieContainer
hreq.CookieContainer = myCookieContainer;
        
string postdata = "id=2005&action=search&name=";
byte[] byte1 = Encoding.ASCII.GetBytes(postdata);
hreq.ContentLength = byte1.Length;
         
Stream poststream = hreq.GetRequestStream();
poststream.Write(byte1, 0, byte1.Length);
poststream.Close();
     
HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();

原文地址:https://www.cnblogs.com/zzh1236/p/2359029.html