zoukankan      html  css  js  c++  java
  • 使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)


       这篇文章是在博客园正式的第一篇文章。


    不出意外 以后将在web的方向发展,前段时间把老早以前做过的webqq机器人重做了一遍,算是对winform的告别吧,巩固了C#方面的知识。

       本篇主要介绍了我对模拟http请求方式的介绍和理解。(博客的样式是自己写的,有木有感觉好看呢(•‾̑⌣‾̑•)✧˖°)

    public string GetHtml(string url, string Referer, Encoding encode, bool SaveCookie)
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.Method = "GET";
                req.CookieContainer = this.CookieContainer;
                req.Proxy = null;
                if (!string.IsNullOrEmpty(Referer))
                    req.Referer = Referer;
                using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
                {
                    if (SaveCookie)
                    {
                        this.CookieCollection = hwr.Cookies;
                        this.CookieContainer.GetCookies(req.RequestUri);
                    }
                    using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
                    {
                        return SR.ReadToEnd();
                    }
                }
            }
    

      

     public string PostHtml(string url, string Referer, string data, Encoding encode, bool SaveCookie)
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.CookieContainer = this.CookieContainer;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0";
                req.Proxy = null;
                req.ProtocolVersion = HttpVersion.Version10;
                if (!string.IsNullOrEmpty(Referer))
                    req.Referer = Referer;
                byte[] mybyte = Encoding.Default.GetBytes(data);
                req.ContentLength = mybyte.Length;
                using (Stream stream = req.GetRequestStream())
                {
                    stream.Write(mybyte, 0, mybyte.Length);
                }
                using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
                {
                    if (SaveCookie)
                    {
                        this.CookieCollection = hwr.Cookies;
                        this.CookieContainer.GetCookies(req.RequestUri);
                    }
                    using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
                    {
                        return SR.ReadToEnd();
                    }
                }
            }
    

    小结

    1.这是我封装的一个httphelp类中的一部分,get貌似不需要那些像什么UserAgent,ContentType之类的东西

    2.POST请求中一般要设置ContentType和UserAgent

    3.默认请求是GET

    每次请求之后保存CookieContainer即可,赋给下一次请求的httpwebrequest
  • 相关阅读:
    window server2019+vmware16+Ubuntu20部署网站记录
    CentOS7源码安装MySQL
    CentOS7源码安装Python、virtualenv虚拟环境安装、uwsgi安装配置
    CentOS7 源码安装Nginx及Nginx基本管理设置
    Ubuntu 64位桌面版 16.04.1 设置桥接模式和固定静态IP方法
    Windows 下日志保存至Linux rsyslog日志服务器
    python 接参数的一个小坑
    历旧服务器配置注意事项
    gitlab设置邮件通知
    Linux基础篇之目录与文件
  • 原文地址:https://www.cnblogs.com/lianmin/p/4227242.html
Copyright © 2011-2022 走看看