GetData

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;

namespace Framework.Common
{
    public class GatherData
    {

        #region 获取指定远程网页内容
        /// <summary>
        /// 获取指定远程网页内容
        /// </summary>
        /// <param name="strUrl">所要查找的远程网页地址</param>
        /// <param name="timeout">超时时长设置,一般设置为8000</param>
        /// <param name="enterType">是否输出换行符,0不输出,1输出文本框换行</param>
        /// <param name="EnCodeType">编码方式</param>
        /// <returns></returns>
        public static string GetRequestString(string strUrl, int timeout, int enterType, Encoding EnCodeType)
        {
            string strResult;
            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                myReq.Timeout = timeout;
                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream();
                StreamReader sr = new StreamReader(myStream, EnCodeType);
                StringBuilder strBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    strBuilder.Append(sr.ReadLine());
                    if (enterType == 1)
                    {
                        strBuilder.Append("\r\n");
                    }
                }
                strResult = strBuilder.ToString();
            }
            catch (Exception err)
            {
                strResult = "请求错误:" + err.Message;
            }
            return strResult;
        }

        /// <summary>
        /// 获取初始网页内容
        /// </summary>
        /// <param name="strLink">网页地址</param>
        /// <returns>网页内容</returns>
        public static string GetRequestString(string strLink)
        {
            if (!strLink.Contains("http://"))
            {
                strLink = "http://" + strLink;
            }
            WebRequest myRequest = WebRequest.Create(strLink);
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader streamReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.Default, true);
            return streamReader.ReadToEnd();
        }

        /// <summary>
        /// 通过WebClient获取初始网页内容
        /// </summary>
        /// <param name="strLink">网页地址</param>
        /// <returns>网页内容</returns>
        public static string GetStrByWebClient(string strLink)
        {
            string result = string.Empty;
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;

            ///方法一:
            Byte[] pageData = wc.DownloadData(strLink);
            result = Encoding.Default.GetString(pageData);
            wc.Dispose();


            ///// 方法二:
            //Stream resStream = wc.OpenRead(strLink);
            //StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            //result = sr.ReadToEnd();
            //resStream.Close();

            return result;
        }
        #endregion

        /// <summary>
        /// 获取初始网页内容
        /// </summary>
        /// <param name="strLink"></param>
        /// <returns></returns>
        public string GetSiteInfo(string strLink)
        {
            WebRequest myRequest = WebRequest.Create(strLink);
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader streamReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.Default, true);
            return streamReader.ReadToEnd();
        }

        private static string PostData(string url, string postdata, CookieContainer cookieContainer)
        {
            string outdata = string.Empty;
           
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postdata.Length;
            request.Method = "POST";
            request.CookieContainer = cookieContainer;
            using (Stream inStream = request.GetRequestStream())
            {
                using (StreamWriter sw = new StreamWriter(inStream, Encoding.GetEncoding("gb2312")))
                {
                    sw.Write(postdata);
                }
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            response.Cookies = cookieContainer.GetCookies(request.RequestUri);
            using (Stream outStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(outStream, Encoding.GetEncoding("gb2312")))
                {
                    outdata = sr.ReadToEnd();
                }
            }

            return outdata;
        }

        private static string GetDate(string url, CookieContainer myCookieContainer)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "text/html";
            request.Method = "GET";
            request.CookieContainer = myCookieContainer;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string outdata = string.Empty;

            if (request.HaveResponse)
            {
                foreach (Cookie returnCookie in response.Cookies)
                {
                    bool cookieFound = false;

                    foreach (Cookie oldCookie in myCookieContainer.GetCookies(request.RequestUri))
                    {
                        if (returnCookie.Name.Equals(oldCookie.Name))
                        {
                            oldCookie.Value = returnCookie.Value;
                            cookieFound = true;
                        }
                    }

                    if (!cookieFound)
                        myCookieContainer.Add(returnCookie);
                }
            }

            using (Stream outStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(outStream, Encoding.GetEncoding("gb2312")))
                {
                    outdata = sr.ReadToEnd();
                }
            }

            return outdata;
        }
    }
}

你可能感兴趣的:(Data)