如何在 C# 中登录网页并检索其内容?
如何在 C# 中登录网页并检索其内容?
How do you login to a webpage and retrieve its content in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 C# 中登录网页并检索其内容?
How do you login to a webpage and retrieve its content in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
这取决于登录所需的内容。您可以使用 Web 客户端将登录凭据发送到服务器的登录页面(通过所需的任何方法,GET 或 POST),但这不会保留 cookie。 有一种方法让网络客户端处理cookie,所以您可以将登录信息发布到服务器,然后使用同一网络客户端请求您想要的页面,然后对该页面执行您想要的任何操作。
That depends on what's required to log in. You could use a webclient to send the login credentials to the server's login page (via whatever method is required, GET or POST), but that wouldn't persist a cookie. There is a way to get a webclient to handle cookies, so you could just POST the login info to the server, then request the page you want with the same webclient, then do whatever you want with the page.
查看
System.Net.WebClient
,或者了解更高级的要求System.Net.HttpWebRequest/System.Net.HttpWebResponse
。至于实际应用这些:您必须研究要抓取的每个页面的 html 源代码,以便准确了解它所期望的 Http 请求。
Look at
System.Net.WebClient
, or for more advanced requirementsSystem.Net.HttpWebRequest/System.Net.HttpWebResponse
.As for actually applying these: you'll have to study the html source of each page you want to scrape in order to learn exactly what Http requests it's expecting.
“登录”是什么意思?
如果子文件夹在操作系统级别受到保护,并且当您访问该子文件夹时浏览器会弹出登录对话框,则您将需要在 HttpWebRequest 上设置 Credentials 属性。
如果网站有自己的基于 cookie 的会员/登录系统,您将必须使用 HttpWebRequest 首先响应登录表单。
How do you mean "login"?
If the subfolder is protected on the OS level, and the browser pops of a login dialog when you go there, you will need to set the Credentials property on the HttpWebRequest.
If the website has it's own cookie-based membership/login system, you will have to use HttpWebRequest to first response to the login form.
使用 WebClient 类。
Use the WebClient class.
您可以使用 WebClient 对象中的构建,而不是自己创建请求。
You can use the build in WebClient Object instead of crating the request yourself.
尝试这个:
Try this: