19
2015
01

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

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»