#region 获取第三方网站内容
//获取其他网站网页内容的关键代码
WebRequest request = WebRequest.Create(第三方的网站地址);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//reader.ReadToEnd() 表示取得网页的源码
string Code = reader.ReadToEnd();
HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(Code);
#region 处理链接地址
HtmlNodeCollection nodes = document.DocumentNode.SelectNodes(@"//a");
if (nodes != null)
{
string strSrc = string.Empty;
foreach (var Item in nodes)
{
string strUrl = Item.GetAttributeValue("href", "");
if (!string.IsNullOrEmpty(strUrl) && strUrl != "#" && strUrl.Contains(".."))
{
strUrl = "原网站的链接" + strUrl.Substring(2);
}
else if (!string.IsNullOrEmpty(strUrl) && strUrl != "#" && !strUrl.Contains(".."))
{
strUrl = "原网站的链接" + strUrl;
}
Item.SetAttributeValue("href", "#");
Item.SetAttributeValue("class", "ininfo");
Item.SetAttributeValue("data-url", strUrl);
//Item.SetAttributeValue("data-original", strSrc);
}
}
#endregion
#region 处理图片地址
nodes = document.DocumentNode.SelectNodes(@"//img");
if (nodes != null)
{
string strSrc = string.Empty;
foreach (var Item in nodes)
{
string strImg = Item.GetAttributeValue("src", "");
if (!strImg.Contains("http"))
{
strImg = "原网站的链接" + strImg;
Item.SetAttributeValue("src", strImg);
}
Item.SetAttributeValue("style", "width:100%");
//Item.SetAttributeValue("data-original", strSrc);
}
}
#endregion
var temp = document.DocumentNode.SelectSingleNode("//div[@class='detail']");
ViewData["contenttext"] = temp.InnerHtml;
#endregion
//获取其他网站网页内容的关键代码
WebRequest request = WebRequest.Create(第三方的网站地址);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//reader.ReadToEnd() 表示取得网页的源码
string Code = reader.ReadToEnd();
HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(Code);
#region 处理链接地址
HtmlNodeCollection nodes = document.DocumentNode.SelectNodes(@"//a");
if (nodes != null)
{
string strSrc = string.Empty;
foreach (var Item in nodes)
{
string strUrl = Item.GetAttributeValue("href", "");
if (!string.IsNullOrEmpty(strUrl) && strUrl != "#" && strUrl.Contains(".."))
{
strUrl = "原网站的链接" + strUrl.Substring(2);
}
else if (!string.IsNullOrEmpty(strUrl) && strUrl != "#" && !strUrl.Contains(".."))
{
strUrl = "原网站的链接" + strUrl;
}
Item.SetAttributeValue("href", "#");
Item.SetAttributeValue("class", "ininfo");
Item.SetAttributeValue("data-url", strUrl);
//Item.SetAttributeValue("data-original", strSrc);
}
}
#endregion
#region 处理图片地址
nodes = document.DocumentNode.SelectNodes(@"//img");
if (nodes != null)
{
string strSrc = string.Empty;
foreach (var Item in nodes)
{
string strImg = Item.GetAttributeValue("src", "");
if (!strImg.Contains("http"))
{
strImg = "原网站的链接" + strImg;
Item.SetAttributeValue("src", strImg);
}
Item.SetAttributeValue("style", "width:100%");
//Item.SetAttributeValue("data-original", strSrc);
}
}
#endregion
var temp = document.DocumentNode.SelectSingleNode("//div[@class='detail']");
ViewData["contenttext"] = temp.InnerHtml;
#endregion