Post/Get方式获取Web页面

阅读更多
浏览器浏览Web页面的时候也无非是 post 或者 get  ,带 cookies 或者不带 cookies 这几种方式。
有了下面这几个方法,什么web页面上的自动投票啊,自动查数据啊等等,全部搞定!
有个工具可以让你查看到IE浏览页面时候的通信内容:httplook , 也顺便推荐一下。


using System.Web;

        ///
        /// Post方式请求页面
        ///

        /// 地址
        /// post数据
        /// 响应
        private HttpWebResponse RequestbyPost(string url, string postData)
        {
            Uri lcUri = new Uri(url);
            HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUri);

            //string lcPostData = HttpUtility.UrlEncode(postData, System.Text.Encoding.Default);
            string lcPostData = postData;
            loHttp.Method = "POST";
            loHttp.ContentType = "application/x-www-form-urlencoded";
            loHttp.KeepAlive = true;
            byte[] lbPostBuffer = System.Text.Encoding.Default
                .GetBytes(lcPostData);
            loHttp.ContentLength = lbPostBuffer.Length;
            Stream loPostData = loHttp.GetRequestStream();
            loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length);
            loPostData.Close();

            return (HttpWebResponse)loHttp.GetResponse();
        }

        ///
        /// Post方式请求页面
        ///

        /// 地址
        /// post数据
        /// 响应
        private HttpWebResponse RequestbyPost(string url, string postData, string cookieHeader)
        {
            Uri lcUri = new Uri(url);
            HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUri);

            //string lcPostData = HttpUtility.UrlEncode(postData, System.Text.Encoding.Default);
            string lcPostData = postData;
            loHttp.Method = "POST";
            loHttp.ContentType = "application/x-www-form-urlencoded";
            CookieContainer cookie1 = new CookieContainer();
            loHttp.CookieContainer = cookie1;
            loHttp.CookieContainer.SetCookies(lcUri, cookieHeader);
            loHttp.KeepAlive = true;
            byte[] lbPostBuffer = System.Text.Encoding.Default
                .GetBytes(lcPostData);
            loHttp.ContentLength = lbPostBuffer.Length;
            Stream loPostData = loHttp.GetRequestStream();
            loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length);
            loPostData.Close();

            return (HttpWebResponse)loHttp.GetResponse();
        }


        ///
        /// Get方式请求页面
        ///

        /// 地址
        /// 响应
        private HttpWebResponse RequestbyGet(string url)
        {
            Uri lcUri = new Uri(url);
            HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUri);
            loHttp.Method = "GET";
            //loHttp.ContentType = "application/x-www-form-urlencoded";

            return (HttpWebResponse)loHttp.GetResponse();
        }

        ///
        /// Get方式请求页面
        ///

        /// 地址
        /// cookieHeader
        /// 响应
        private HttpWebResponse RequestbyGet(string url, out string cookieHeader)
        {
            Uri lcUri = new Uri(url);
            HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUri);
            loHttp.Method = "GET";
            //loHttp.ContentType = "application/x-www-form-urlencoded";
            loHttp.AllowAutoRedirect = true;
            loHttp.CookieContainer = new CookieContainer();
            loHttp.KeepAlive = true;
            HttpWebResponse httpWebResponse = (HttpWebResponse)loHttp.GetResponse();
            cookieHeader = loHttp.CookieContainer.GetCookieHeader(lcUri);

            return httpWebResponse;
        }

你可能感兴趣的:(Web,IE,浏览器)