C#获取Html代码

2019-03-10 更新

获得一个网址的Html代码

        /// <summary>
        /// 获取网页html代码 
        /// </summary>
        /// <param name="url">如http://www.luofenming.com 注意一定要加 http:或https:</param>
        /// <returns></returns>
        private string GetHtml(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream stream = response.GetResponseStream())
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        string str = sr.ReadToEnd();
                        return str;
                    }
                }
                else
                {
                    return response.StatusCode.ToString();
                }
            }
        }
关于http协议原理(www服务响应过程)响应细节,报文细节请防问http://www.luofenming.com/show.aspx?id=ART2019031000001

评论