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 2016-09-08 15:40  lopengye  阅读(1922)  评论(0编辑  收藏  举报

导航