Get Web Page - CSharp System.Web

CSharp examples for System.Web:Web Page

Description

Get Web Page

Demo Code


using System.Net;
using System.IO;//from  w ww.ja v  a  2  s .  c  o m
using System.Text.RegularExpressions;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System;

public class Main{
        public static string GetWebPage(string sUrl, bool compress, Encoding enc)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sUrl);
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";

            HttpWebResponse res;

            try
            {
                res = (HttpWebResponse)req.GetResponse();
            }
            catch
            {
                return "Error";
            }

            if (res.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr = new StreamReader(res.GetResponseStream(), enc);
                string sHtml = sr.ReadToEnd();
                sr.Close();
                res.Close();

                if (compress == true)
                {
                    sHtml = CompressHTML(sHtml);
                }

                return sHtml;
            }
            else
            {
                return "Error";
            }
        }
        public static string GetWebPage(string sUrl, bool compress)
        {
            Encoding enc = Encoding.GetEncoding("GB2312");
            return GetWebPage(sUrl, compress, enc);
        }

        public static string GetWebPage(string sUrl)
        {
            return GetWebPage(sUrl, true);
        }
}

Related Tutorials