Html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="获取网页源代码" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
.Aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//收录开始处
string CodeSite = this.Get_Http("http://www.baidu.com/baidu?word=site%3A" + TextBox1.Text+"", 100);
//throw new ApplicationException(CodeSite);
string PrintSite="";
if (CodeSite.IndexOf("找到相关网页") > -1)
{
PrintSite = GetSourceCode(CodeSite, "找到相关网页", "篇", "找到相关网页");
PrintSite = PrintSite.Replace("约", "");
PrintSite = PrintSite.Replace("共", "");
PrintSite = PrintSite.Replace(" ", "");
PrintSite = "收录数量: " + PrintSite + " 页";
PrintSite = PrintSite + " <a href=/"http://www.baidu.com/baidu?word=site%3A" + TextBox1.Text + "/" target=/"_blank/">查看详细</a>";
}
else {
PrintSite="收录数量: 0 页,请点击<a href=/"http://www.baidu.com/search/url_submit.html/" target=/"_blank/">这里</a>登录您的网站。";
}
//收录结束处
//反链开始处
string UrlLink="http://www.baidu.com/baidu?word=domain%3A"+ TextBox1.Text+"";
string CodeLink=Get_Http(UrlLink,100);
string PrintLink="";
if (CodeLink.IndexOf("找到相关网页")>-1){
PrintLink = GetSourceCode(CodeLink,"找到相关网页","篇","找到相关网页");
PrintLink = PrintLink.Replace("约","");
PrintLink = PrintLink.Replace("共","");
PrintLink = PrintLink.Replace(" ","");
PrintLink = "链接数量: "+PrintLink+" 页";
PrintLink = PrintLink + " <a href=/"http://www.baidu.com/baidu?word=domain%3A" + TextBox1.Text + "/" target=/"_blank/">查看详细</a>";
}
else
{
PrintLink="链接数量: 0 页";
}
//反链结束处
string PrintStr = PrintSite+" | "+PrintLink;
Label1.Text = PrintStr.ToString();
}
public string Get_Http(string a_strUrl, int timeout)
{
string strResult;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
myReq.Timeout = timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
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();
}
catch (Exception exp)
{
strResult = "错误:" + exp.Message;
}
return strResult;
}
public string GetSourceCode(string SourceCode_0, string SourceCode_1, string SourceCode_2, string SourceCode_3)
{
StringBuilder tmp = new StringBuilder();
int first = SourceCode_0.IndexOf(SourceCode_1);
int between = first - SourceCode_0.IndexOf(SourceCode_2, first + 1);
tmp.Append(SourceCode_0.Substring(first, Math.Abs(between)).Replace(SourceCode_3, ""));
return tmp.ToString();
}
}