十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这期内容当中小编将会给大家带来有关利用Asp.Ne怎么对搜索引擎网址进行收录检查,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

实现原理:直接搜索你那篇文章的url地址(不带协议,但上协议也行,代码会自动去掉协议内容),如果被索引会返回搜索结果,否则会提示找不到信息。
Asp.Net检查百度,谷歌,搜狗搜索引擎是否收录文章网址源代码:
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Web;
public class SearchEngineIndex
{
public static string[] urls = { //搜索引擎检查地址
"http://www.baidu.com/s?ie=utf-8&wd=",//百度索引url检查地址
"https://www.google.com.hk/search?q=",//谷歌索引url检查地址
"http://www.sogou.com/web?ie=utf8&query="//搜狗索引url检查地址
}
, noFindKeyword = { "抱歉,没有找到与", "找不到和您的查询", "未收录?" };//搜索引擎未索引url地址时的关键字
///
/// 获取响应的编码
///
///
///
private static Encoding GetEncoding(string contenttype)
{
if (!string.IsNullOrEmpty(contenttype))
{
contenttype = contenttype.ToLower();
if (contenttype.IndexOf("gb2312") != -1 || contenttype.IndexOf("gbk") != -1) return Encoding.GetEncoding(936);
if (contenttype.IndexOf("big5") != -1) return Encoding.GetEncoding(950);
}
return Encoding.UTF8;
}
///
/// 使用HttpWebRequest对象,自动识别字符集
///
///
/// 是否添加UserAgent,采集其他网站时防止被拦截
///
public static string GetHtml(string url, bool addUseragent)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
if (addUseragent) request.UserAgent = "Googlebot|Feedfetcher-Google|Baiduspider";
string html = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srd = new StreamReader(response.GetResponseStream(), GetEncoding(response.ContentType));
html = srd.ReadToEnd();
srd.Close();
response.Close();
}
catch { }
return html;
}
///
/// 检查某个url是否被搜索引擎索引
///
/// url地址
/// 0:百度 1:谷歌 2:搜狗,其他搜索引擎如bing和360直接查网址显示的结果不是直接得到网址的,有些出入,不做检查
///
public static bool CheckIndex(string url, int engin)
{
if (string.IsNullOrEmpty(url)) return false;
if (engin < 0 || engin > 2) engin = 0;
url = urls[engin] + HttpUtility.UrlEncode(url.ToLower().Replace("http://", "").Replace("https://", ""));
bool r = true;
string html = GetHtml(url, true);
if (html == null || html.IndexOf(noFindKeyword[engin]) != -1) r = false;
return r;
}
}
//调用方法示例
SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 0);//检查百度索引
SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 1);//检查谷歌索引
SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 2);//检查搜狗索引