gpt4 book ai didi

c# - 使用 C# 中的 asp.net 表单登录屏幕抓取站点?

转载 作者:太空狗 更新时间:2023-10-29 20:12:14 24 4
gpt4 key购买 nike

是否可以为受表单登录保护的网站编写屏幕抓取程序。当然,我可以访问该站点,但我不知道如何登录该站点并将我的凭据保存在 C# 中。

另外,如果有任何用 C# 编写的屏幕抓取器的好例子,我们将不胜感激。

这已经完成了吗?

最佳答案

这很简单。您需要自定义登录 (HttpPost) 方法。

你可以想出这样的事情(这样你会在登录后获得所有需要的cookie,你只需要将它们传递给下一个HttpWebRequest):

public static HttpWebResponse HttpPost(String url, String referer, String userAgent, ref CookieCollection cookies, String postData, out WebHeaderCollection headers, WebProxy proxy)
{
try
{
HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.Proxy = proxy;
http.AllowAutoRedirect = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
http.UserAgent = userAgent;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(cookies);
http.Referer = referer;
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
headers = http.Headers;
cookies.Add(httpResponse.Cookies);

return httpResponse;
}
catch { }
headers = null;

return null;
}

关于c# - 使用 C# 中的 asp.net 表单登录屏幕抓取站点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/901045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com