C# 抓取页面(带认证) 转

[C#]代码

01HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
02req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
03req.Method = "POST";
04req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
05req.Headers.Add("Accept-Language: en-us,en;q=0.5");
06req.Headers.Add("Accept-Encoding: gzip,deflate");
07req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
08req.KeepAlive = true;
09req.Headers.Add("Keep-Alive: 300");
10req.Referer = "copy from url";
11 
12req.ContentType = "application/x-www-form-urlencoded";
13 
14String Username = copy from url;
15String PassWord = copy from url;
16 
17StreamWriter sw = new StreamWriter(req.GetRequestStream());
18sw.Write(string.Format("&loginname={0}&password={1}&btnSubmit=Log In&institutioncode=H4V9KLUT45AV&version=2", Username, PassWord));
19sw.Close();
20HttpWebResponse response = (HttpWebResponse)req.GetResponse();
21 
22StreamReader reader = new StreamReader(response.GetResponseStream());
23string tmp = reader.ReadToEnd();

[代码] Cookie 处理

01CookieCollection cookiesResponse = new CookieCollection();
02 
03if (response != null)
04{
05    foreach (string cookie in response.Headers["Set-Cookie"].Split(';'))
06    {
07        string name = cookie.Split('=')[0];
08        string value = cookie.Substring(name.Length + 1);
09        cookiesResponse.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
10    }
11}
posted @   过错  阅读(229)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示