网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest这三个。
以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:- HtmlElement ClickBtn =null;
- if (e.Url.ToString().ToLower().IndexOf("http://hi.baidu.com/srxljl") > 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://hi.baidu.com/srxljl")); //打开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://hi.baidu.com/srxljl"), "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 string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
- {
- string ret = string.Empty;
- try
- {
- byte[] byteArray = dataEncode.GetBytes(paramData); //转化
- HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
- webReq.Method = "POST";
- webReq.ContentType = "application/x-www-form-urlencoded";
- webReq.ContentLength = byteArray.Length;
- Stream newStream = webReq.GetRequestStream();
- newStream.Write(byteArray, 0, byteArray.Length);//写入参数
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
- ret = sr.ReadToEnd();
- sr.Close();
- response.Close();
- newStream.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- return ret;
- }
复制代码 |