C#代码处理网页关于登录的code

作者:血饮狂龙
链接:https://www.zhihu.com/question/49452639/answer/117294801
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

private string getHtml(string url)
        {
            HttpItem item = new HttpItem()
            {
                URL = url,
                Encoding = null,
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                KeepAlive = false,
                UserAgent = userAgent,
                Expect100Continue = true,
                Header = new WebHeaderCollection()
                {
                                {"Accept-Encoding","gzip, deflate"},
                                {"Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"}
                }
            };
            HttpResult result = http.GetHtml(item);
            return result.Html;

        }
以上代码是再入一个普通网页的代码,采用get方法。下面再贴一段采用post方法的代码:
 private string postHtml(string url, string postData, string referer)
        {
            HttpItem item = new HttpItem()
            {
                URL = url,
                Encoding = null,
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                KeepAlive = true,
                ContentType = "application/x-www-form-urlencoded",
                Referer = referer,
                UserAgent = userAgent,
                Expect100Continue = false,
                Method = "POST",
                Postdata = postData,
                Header = new WebHeaderCollection()
                {
                                {"Accept-Encoding","gzip, deflate"},
                                {"Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"}
                }
            };
            HttpResult result = http.GetHtml(item);
            return result.Html;

        }
上面这段代码一般用于登录之类的提交请求的动作,比如登录。
下面提供一点儿干货:
想学习爬虫的童鞋,你们一定要看看这个网站:[C#HttpHelper]官方产品发布与源码下载 苏菲论坛,这个人把登录网页封装起来用,很方便哟。
你需要用到的工具有:httpwatch或者fiddler抓取网页数据。
win10的亲们,你们需要被特别照顾一下,因为那蛋疼的ie并不兼容httpwatch。firefox或者chrome目前对插件也管控特别严,因此你们需要用到以下组合:
firefox 35.0b8+Httpwatch9.4.17

posted on   lopengye  阅读(1927)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示