Asp.Net获得网站排名信息(WebRequest)

本文介绍了一个使用ASP.NET实现的简单网页,能够查询指定网站在百度上的收录数量及反向链接数量。通过输入目标网址,网页会显示该网站被百度收录的页面数以及指向该网站的所有反向链接数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值