1、最近研究 paperpass 论文检测系统 登录的时候 考虑到需要把登录信息保存下来,http:/ 但是 c#不想 php,有这个功能,不能把cookies 保存文本,并发送到服务器,所以就写了一段代码显示 get 的方式获取网页的源代码 1 / 2 / 获取页面的 html 代码3 / 4 / 地址5 / 来路6 / cookies7 / 8 protected string getHtml(string URL, string Referer, string CookiesFile ,bool AutoRe)/获取指定地址的源文件9 10 HttpWebRequest getRequest
2、 = (HttpWebRequest)WebRequest.Create(URL);11 CookieContainer Cookies = null;12 if (!String.IsNullOrEmpty(CookiesFile)15 16 getRequest.CookieContainer = Cookies;17 18 getRequest.Accept = “*/*“;19 getRequest.UserAgent = “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;)“;20 getRequest.KeepAlive = fa
3、lse;21 getRequest.AllowAutoRedirect = AutoRe;22 if (!String.IsNullOrEmpty(Referer)23 24 getRequest.Referer = Referer;25 26 try27 28 HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();29 string LocationUlr = getResponse.Headers.GetValues(“Location“);30 http:/ 31 if (Cookies != nu
4、ll 34 /保存 Cookies 文件35 saveCookieContainer(Cookies, CookiesFile);36 37 /如果是转向的38 if (!AutoRe 41 return LocationUlr0;42 43 StreamReader readStream = new StreamReader(getResponse.GetResponseStream(), Encoding.UTF8);44 var TheHtml = readStream.ReadToEnd();45 readStream.Close(); readStream.Dispose(); 46
5、 getResponse.Close();47 48 return TheHtml;49 50 catch (Exception E)51 52 writeLog(E.ToString(), ownsHttpContext.Server.MapPath(“/“);53 return null;54 55 finally56 57 getRequest.Abort();58 59 其中的 getCookieContainer 函数 从文件读取 CookieContainer ,saveCookieContainer 函数把 CookieContainer 保存到文件!两个函数如下 / / 遍历
6、CookieContainer/ / / public static List GetAllCookies(CookieContainer cc)List lstCookies = new List();Hashtable table = (Hashtable)cc.GetType().InvokeMember(“m_domainTable“,System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |System.Reflection.BindingFlags.Instance, nu
7、ll, cc, new object );foreach (object pathList in table.Values)SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember(“m_list“,System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField| System.Reflection.BindingFlags.Instance, null, pathList, new object );forea
8、ch (CookieCollection colCookies in lstCookieCol.Values)foreach (Cookie c in colCookies) lstCookies.Add(c);return lstCookies;/ / 把 cookieContainer 保存到文件/ / 需要写入的 CookieContainer/ 保存到什么文件 private static void saveCookieContainer(CookieContainer Cookies, string cookieFile)StringBuilder SB = new StringBu
9、ilder();if (Cookies = null | Cookies.Count = 0) return; /检测目录是否存在if (!File.Exists(cookieFile)FileInfo cookieFileInfo = new FileInfo(cookieFile);if (!cookieFileInfo.Directory.Exists)cookieFileInfo.Directory.Create();List CClist = GetAllCookies(Cookies);foreach (Cookie Cookie in CClist)SB.AppendFormat
10、(“0|,|1|,|2|,|3|,|4rn“, Cookie.Name, Cookie.Value, Cookie.Expires.Ticks, Cookie.Domain, Cookie.Path);File.WriteAllText(cookieFile, SB.ToString();/ / 从文件获取 cookies/ / cookie 保存的文件/ private static CookieContainer getCookieContainer(string cookieFile)CookieContainer returnCookieContainer = new CookieCo
11、ntainer();if (!File.Exists(cookieFile)return returnCookieContainer;string CookieStrS = File.ReadAllLines(cookieFile);foreach (string CookieStr in CookieStrS)string TempStrS = CookieStr.Split(new string “|,|“ , StringSplitOptions.RemoveEmptyEntries);if (TempStrS.Length 5)continue;Cookie CookieTemp = new Cookie();CookieTemp.Name = TempStrS0;CookieTemp.Value = TempStrS1;CookieTemp.Expires = new DateTime(Convert.ToInt64(TempStrS2);CookieTemp.Domain = TempStrS3;CookieTemp.Path = TempStrS4;returnCookieContainer.Add(CookieTemp);return returnCookieContainer;