C#在WinFrom中Get/Post请求及QQ农场Cookie保存

本文地址:http://www.cnblogs.com/atree/archive/2010/04/20/WinFrom-GetPost-Cookie.html

注意Cookie的绑定,此处Cookie为获取验证码时记录的Cookie值。

 #region 提交数据方法
        // 公用Cookie信息
        public static CookieContainer myCookie;
        //返回的Html代码或结果
        public static string html;
        /// <summary>
        /// Get方式提交
        /// </summary>
        /// <param name="strUrl">目的资源链接</param>
        /// <returns>String:Response返回值</returns>
        public static string GetModel(string strUrl, string server)
        {
            string strRet = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Timeout = 2000; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream resStream = response.GetResponseStream();
                Encoding encode = System.Text.Encoding.UTF8;
                StreamReader readStream = new StreamReader(resStream, encode);
                Char[] read = new Char[256];
                int count = readStream.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    strRet = strRet + str;
                    count = readStream.Read(read, 0, 256);
                }
                resStream.Close();
            }
            catch (Exception e)
            {
                strRet = "";
            }
            return strRet;
        }

        /// <summary>
        /// 绑定Cookie、获取验证码
        /// </summary>
        /// <param name="strUrl">链接地址(验证码获取地址)</param>
        /// <param name="server">Referer地址</param>
        /// <returns>Stream:验证码的数据流</returns>
        public static Stream GetModelStream(string strUrl, string server)//, string proxy, bool isProxy)
        {
            System.IO.Stream resStream = null;
            string cookie;
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                httpRequest.Timeout = 2000;
                httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpRequest.Referer = server;
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                cookie = httpResponse.Headers.Get("Set-Cookie");
                myCookie = new CookieContainer();
                myCookie.SetCookies(new Uri(server), cookie);
                resStream = httpResponse.GetResponseStream();
            }
            catch { }
            return resStream;
        }

        /// <summary>
        /// Post方法提交
        /// </summary>
        /// <param name="strUrl">目的资源链接</param>
        /// <param name="strParm">传递的参数和值</param>
        /// <returns>String:Response返回值</returns>
        public static string PostModel(string strUrl, string server, string strParm)
        {
            Encoding encode = System.Text.Encoding.UTF8;
            byte[] arrB = encode.GetBytes(strParm);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.Timeout = 2000;
            myReq.Referer = server;
            myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            myReq.ContentLength = arrB.Length;
            myReq.CookieContainer = myCookie;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            WebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应       
                myResp = myReq.GetResponse();
            }
            catch (Exception e)
            {
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();
            return str;
        }
        #endregion
posted on 2010-04-20 01:02  zock  阅读(3806)  评论(2编辑  收藏  举报