首页 > 学院 > 开发设计 > 正文

使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)

2019-11-17 02:37:50
字体:
来源:转载
供稿:网友

使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)


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


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

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

首先 看一个GET请求

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();                }            }        }

然后 再看POST

 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

如何 保持Cookie

每次请求之后保存CookieContainer即可,赋给下一次请求的httpwebrequest

区别 HttpWebRequest.CookieContainer和HttpWebResponse.CookieCollection

此处说的是进行一次http请求获得响应之后

CookieContainer是当前域的所有Cookie

CookieCollection是该次请求相关的所有Cookie

Referer 是什么

即本次请求的来源,从哪个页面进行http请求的

这里常被服务器用来检测请求是否合法

proxy?

请求的代理,对此我了解不多,忘有人能告之

在请求之前设置proxy=null,可以让请求跳过检查代理,http请求速度立刻上升一个档次!尤其是以前蹭wifi的时候我深有体会


这里是我封装的一个http请求辅助类,大家有兴趣可以参考一下

public class HttpHelp    {        public CookieContainer CookieContainer { get; set; }        public CookieCollection CookieCollection { get; set; }        public HttpHelp()        {            this.CookieCollection = new CookieCollection();            this.CookieContainer = new CookieContainer();        }        public static string GetHtml(string url, string Referer, Encoding encode)        {            return new HttpHelp().GetHtml(url, Referer, encode, false);        }        public static string PostHtml(string url, string Referer, string data, Encoding encode)        {            return new HttpHelp().PostHtml(url, Referer, data, encode, false);        }        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();                }            }        }        ///        /// 上传文件        ///        ///上传地址        ///文件路径        ///原网页file控件name        ///请求流中的contentType        ///返回的encoding        ///post参数字典        ///         public static string PostFile(string url, string filepath, string paramName, string contentType, Encoding encode, Dictionary<string, string> dict)        {            HttpWebRequest hrq = WebRequest.Create(url) as HttpWebRequest;            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");            byte[] boundarybytes = System.Text.Encoding.Default.GetBytes("/r/n--" + boundary + "/r/n");            hrq.ContentType = "multipart/form-data; boundary=" + boundary;            hrq.Method = "POST";            using (Stream stream = hrq.GetRequestStream())   //请求流            {                //写入post参数                string formdataTemplete = "Content-Disposition: form-data; name=/"{0}/"/r/n/r/n{1}";                if (dict != null && dict.Count > 0)                {                    foreach (KeyValuePair<string, string> pair in dict)                    {                        stream.Write(boundarybytes, 0, boundarybytes.Length);                        string formitem = string.Format(formdataTemplete, pair.Key, pair.Value);                        byte[] formitemBytes = Encoding.Default.GetBytes(formitem);                        stream.Write(formitemBytes, 0, formitemBytes.Length);                    }                }                stream.Write(boundarybytes, 0, boundarybytes.Length);                //写入头信息                string headerTemplate = "Content-Disposition: form-data; name=/"{0}/"; filename=/"{1}/"/r/nContent-Type: {2}/r/n/r/n";                string header = string.Format(headerTemplate, paramName, Path.GetFileName(filepath), contentType);                byte[] headerBytes = Encoding.UTF8.GetBytes(header);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表