日期:2014-05-18  浏览次数:20489 次

怎样获取一个网页本身的html代码?
如我在A.aspx 怎样获取B.aspx的html的代码。如我们用记事本察看网页,获取的那些代码一样。

------解决方案--------------------
 
string url="http://163.ifcast.ifocus.cn/";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default))
{
return sr.ReadToEnd();
}
}
}
catch (System.Exception e)
{
return @"Error";
}
finally
{
req.Abort();
}
}
------解决方案--------------------
public static string GetHttpSourceValue(string a_strUrl)
{

string strResult;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(new System.Uri(a_strUrl));



myReq.Method = "GET";
myReq.Accept = "*/*";
myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";//下载网页源码
// System.Threading.Thread.Sleep(1000);
try
{
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();

string tmp = myReq.Headers.ToString();

Stream myStream = HttpWResp.GetResponseStream();

StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 < sr.Peek())
{
strBuilder.Append(sr.ReadLine() + "\r\n");
}

strResult = strBuilder.ToString();
// StreamWriter sw = new StreamWriter("E:\\1.txt", false, Encoding.Default);
// sw.Write(strResult);


myStream.Close();
sr.Close();
// sw.Close();
}
catch (Exception exp)
{
strResult = "错误:" + exp.Message;
}


return strResult;




// StreamWriter sw = new StreamWriter(SaveFileName(), false, Encoding.Default);
// sw.Write(body);
// sw.Close();
}
------解决方案--------------------
C# code

 /// <summary>
        /// 获取网页源文件
        /// </summary>
        /// <param name="url">网页地址</param>
        /// <param name="charset">网页编码类型 如:gb2312</param>
        /// <returns>返回网页源文件</returns>
        public static string GetSnatchHtml(string url, string charset)
        {
            string pageHtml = "";
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                myHttpWebRequest.Method = "GET";
                myHttpWebRequest.Timeout = 30000;   //设置超时时间

                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                Stream receiveStream = myHttpWebResponse.GetResponseStream();
                StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding(charset.ToLower()));
                string returnVal = readStream.ReadToEnd().Trim();
                readStream.Close();
                receiveStream.Close();
                return returnVal;
            }
            catch (Exception ee)
            {
                string s = url + ":" + ee.Message + ";" + ee.Source + ";" + ee.StackTrace + ";";