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
  • 相关阅读:
    Linux常用操作命令总结
    Centos7安装FastDFS教程
    Permutation Sequence
    Next Permutation
    Remove Element
    4Sum
    3Sum Closest
    3Sum
    Longest Consecutive Sequence
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/lianmin/p/4227242.html
Copyright © 2011-2022 走看看