-
大小: 182KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-18
- 語言: 其他
- 標簽: Webrequest??http??request??C#??
資源簡介
這是一個Webrequests模擬登錄 ,獲取網(wǎng)頁源碼,提取cookie,并提取驗證碼進行登錄驗證的示例,適合初學者

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.IO;
using?System.Collections;
using?System.Web;
namespace?MyWebRequest
{
????public?partial?class?Form1?:?Form
????{
????????public?CookieContainer?cookieContainer;
????????protected?static?string?cookieheader;
????????public?Form1()
????????{
????????????InitializeComponent();
????????????loadData();
????????}
????????private?void?loadData()
????????{
?????????
????????????cookieContainer?=?new?CookieContainer();
????????????string?URI?=?“http://w9.s1.ab1999.net/login.php“;
????????????//?設置打開頁面的參數(shù)
?????????
????????????HttpWebRequest?request?=?WebRequest.Create(URI)?as?HttpWebRequest;
????????????request.Method?=?“GET“;
????????????request.KeepAlive?=?false;
????????????request.CookieContainer?=?cookieContainer;
????????????//?接收
????????????HttpWebResponse?response?=?request.GetResponse()?as?HttpWebResponse;
????????????cookieContainer.Add(response.Cookies);
????????????response.Close();
????????????URI?=?“http://w9.s1.ab1999.net/codeimg.php“;
????????????DowloadCheckImg(URI?cookieContainer);
???????????
????????}
///???
///?下載驗證碼圖片并picturebox1顯示??
///? ??
///?驗證碼URL??
///?Cookies值??
public??bool?DowloadCheckImg(string?Url?CookieContainer?cookCon)
{
????bool?bol?=?true;
????HttpWebRequest?webRequest?=?(HttpWebRequest)WebRequest.Create(Url);
????//屬性配置??
????webRequest.Credentials?=?System.Net.CredentialCache.DefaultCredentials;
??webRequest.Accept?=?“image/gif?image/x-xbitmap?image/jpeg?image/pjpeg?application/x-shockwave-flash?application/vnd.ms-excel?application/vnd.ms-powerpoint?application/msword?*/*“;
????webRequest.UserAgent?=?“Mozilla/4.0?(compatible;?MSIE?6.0;?Windows?NT?5.1;?SV1;?Maxthon;?.NET?CLR?1.1.4322)“;
???//?webRequest.ContentType?=?“application/x-www-form-urlencoded“;
????webRequest.Method?=?“GET“;
???//?webRequest.Headers.Add(“Accept-Language“?“zh-cn“);
???//?webRequest.Headers.Add(“Accept-Encoding“?“gzipdeflate“);
????webRequest.Referer?=?“http://w9.s1.ab1999.net/login.php“;
????webRequest.KeepAlive?=?false;
????webRequest.CookieContainer?=?cookieContainer;
????
????try
????{
????????//獲取服務器返回的資源??
????????using?(HttpWebResponse?webResponse?=?(HttpWebResponse)webRequest.GetResponse())
????????{
??????????//??cookieContainer.Add(webResponse.Cookies);
????????????using?(Stream?sream?=?webResponse.GetResponseStream())
????????????{
????????????????pictureBox1.Image?=?System.Drawing.Image.FromStream(sream);
????????????????
????????????}
????????}
????}
????catch?(WebException?ex)
????{
????????bol?=?false;
????}
????catch?(Exception?ex)
????{
????????bol?=?false;
????}
????return?bol;
}
????????//開始登錄
????????private?void?btnNext_Click(object?sender
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1128??2011-04-18?12:42??MyWebRequest\bin\Debug\image
?????文件?????212992??2011-04-18?12:25??MyWebRequest\bin\Debug\Interop.MSxm
?????文件?????139264??2011-04-18?22:50??MyWebRequest\bin\Debug\Interop.SHDocVw.dll
?????文件??????32768??2011-04-19?09:29??MyWebRequest\bin\Debug\MyWebRequest.exe
?????文件??????44544??2011-04-19?09:29??MyWebRequest\bin\Debug\MyWebRequest.pdb
?????文件???????5632??2011-04-19?09:29??MyWebRequest\bin\Debug\MyWebRequest.vshost.exe
?????文件??????12125??2011-04-19?00:06??MyWebRequest\Form1.cs
?????文件???????9395??2011-04-18?23:12??MyWebRequest\Form1.Designer.cs
?????文件???????5814??2011-04-18?23:12??MyWebRequest\Form1.resx
?????文件???????4164??2011-04-19?09:28??MyWebRequest\FrmAfterLogin.cs
?????文件???????5219??2011-04-19?00:58??MyWebRequest\FrmAfterLogin.Designer.cs
?????文件???????5814??2011-04-19?00:58??MyWebRequest\FrmAfterLogin.resx
?????文件???????4636??2011-04-18?23:12??MyWebRequest\MyWebRequest.csproj
?????文件????????604??2011-04-17?23:43??MyWebRequest\MyWebRequest.csproj.user
?????文件?????212992??2011-04-18?12:25??MyWebRequest\obj\Debug\Interop.MSxm
?????文件?????139264??2011-04-18?22:50??MyWebRequest\obj\Debug\Interop.SHDocVw.dll
?????文件????????973??2011-04-19?00:58??MyWebRequest\obj\Debug\MyWebRequest.csproj.GenerateResource.Cache
?????文件????????542??2011-04-18?22:50??MyWebRequest\obj\Debug\MyWebRequest.csproj.ResolveComReference.cache
?????文件??????32768??2011-04-19?09:29??MyWebRequest\obj\Debug\MyWebRequest.exe
?????文件????????180??2011-04-18?23:12??MyWebRequest\obj\Debug\MyWebRequest.Form1.resources
?????文件????????180??2011-04-19?00:58??MyWebRequest\obj\Debug\MyWebRequest.FrmAfterLogin.resources
?????文件??????44544??2011-04-19?09:29??MyWebRequest\obj\Debug\MyWebRequest.pdb
?????文件????????180??2011-04-18?23:12??MyWebRequest\obj\Debug\MyWebRequest.Properties.Resources.resources
?????文件????????180??2011-04-18?23:12??MyWebRequest\obj\Debug\MyWebRequest.validateCode.resources
?????文件???????1447??2011-04-19?09:29??MyWebRequest\obj\MyWebRequest.csproj.FileListAbsolute.txt
?????文件????????471??2011-04-19?09:29??MyWebRequest\Program.cs
?????文件???????1172??2011-04-17?22:36??MyWebRequest\Properties\AssemblyInfo.cs
?????文件???????2880??2011-04-17?22:36??MyWebRequest\Properties\Resources.Designer.cs
?????文件???????5350??2011-04-17?22:36??MyWebRequest\Properties\Resources.resx
?????文件???????1097??2011-04-17?22:36??MyWebRequest\Properties\Settings.Designer.cs
............此處省略17個文件信息
評論
共有 條評論