C#推送URL链接到百度搜索资源平台快速收录URL网址
作者:C/S框架网|www.cscode.ne  发布日期:2021/03/04 11:49:58
  C#推送URL链接到百度搜索资源平台快速收录URL网址


C#推送链接URL到百度搜索资源平台提高收录量

百度站长平台链接提交的主动推送(实时)是最为快速的提交方式,推荐您将站点当天新产出链接立即通过此方式推送给百度,以保证新链接可以及时被百度收录。






C# Code:

/// <summary>
/// C#推送链接URL到百度搜索资源平台提高收录量
/// </summary>
public class BaiduUrlRecorder
{
  
/// <summary>
  
/// 百度推送接口调用地址
  
/// </summary>
  
private const string api_url_post = "http://data.zz.baidu.com/urls?site={0}&token={1}";
  
private const string api_url_del = "http://data.zz.baidu.com/urls?site={0}&token={1}";
  
private const string api_url_update = "http://data.zz.baidu.com/urls?site={0}&token={1}";
  
  
string _siteName = "";
  
string _token = "";
  
  
static BaiduUrlRecorder _Instance = null;
  
  
public static BaiduUrlRecorder Instance
  {
    
get
    {
      
if (_Instance == null)
      _Instance
= new BaiduUrlRecorder("www.systables.com", "80MIjtCZFtVbQe3kP");
      
      
return _Instance;
    }
  }
  
  
public BaiduUrlRecorder(string siteName, string token)
  {
    _siteName
= siteName;
    _token
= token;
  }
  
  
/// <summary>
  
/// 推送链接URL到百度搜索资源平台
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public PostUrlToBaiduResponse Post(string url)
  {
    
return PostUrl(string.Format(api_url_post, _siteName, _token), url);
  }
  
  
/// <summary>
  
/// 更新链接url
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public PostUrlToBaiduResponse Update(string url)
  {
    
return PostUrl(string.Format(api_url_update, _siteName, _token), url);
  }
  
  
/// <summary>
  
/// 删除链接url
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public PostUrlToBaiduResponse Delete(string url)
  {
    
return PostUrl(string.Format(api_url_del, _siteName, _token), url);
  }
  
  
/// <summary>
  
/// POST方式调用百度站长平台(资源搜索平台)API接口
  
/// </summary>
  
/// <param name="baidu_api_url">百度API接口</param>
  
/// <param name="my_url">URL地址</param>
  
/// <returns></returns>
  
private PostUrlToBaiduResponse PostUrl(string baidu_api_url, string my_url)
  {
    Stream outstream
= null;
    Stream instream
= null;
    StreamReader sr
= null;
    HttpWebResponse response
= null;
    HttpWebRequest request
= null;
    
    
// 准备请求...
    
try
    {
      Encoding encoding
= System.Text.Encoding.GetEncoding("gb2312");
      
byte[] data = encoding.GetBytes(my_url);
      
      
// 设置参数
      
request = WebRequest.Create(baidu_api_url) as HttpWebRequest;
      
      CookieContainer cookieContainer
= new CookieContainer();
      request.UserAgent
= "curl/7.12.1";
      request.CookieContainer
= cookieContainer;
      request.AllowAutoRedirect
= true;
      request.Method
= "POST";
      request.ContentType
= "text/plain";
      request.ContentLength
= data.Length;
      
      outstream
= request.GetRequestStream();
      outstream.Write(data,
0, data.Length);
      outstream.Close();
      
      
//发送请求并获取相应回应数据
      
response = request.GetResponse() as HttpWebResponse;
      
//直到request.GetResponse()程序才开始向目标网页发送Post请求
      
instream = response.GetResponseStream();
      sr
= new StreamReader(instream, encoding);
      
      
//返回结果网页(html)代码
      
string content = sr.ReadToEnd();
      
string err = string.Empty;
      
      PostUrlToBaiduResponse ret
= Newtonsoft.Json.JsonConvert.DeserializeObject<PostUrlToBaiduResponse>(content);
      
return ret;
    }
    
catch (Exception ex)
    {
      
//提交异常,出错原因有:token is not valid,远程服务器返回错误: (401) 未经授权。
      
return new PostUrlToBaiduResponse { error = -1, message = ex.Message };
    }
  }
}


//来源:C/S框架网 | www.csframework.com | QQ:23404761




C# Code:


/// <summary>
/// 百度服务器回应
/// </summary>
public class PostUrlToBaiduResponse
{
  
public int remain { get; set; }
  
public int success { get; set; }
  
public int error { get; set; }
  
public string message { get; set; }
  
  
public override string ToString()
  {
    
return $"remain:{remain},success:{success},error:{error},message:{message}";
  }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




推送1条URL:


C# Code:

//推送百度收录
string urlTmp = "http://www.systables.com/tables/show-21030317534215466809.html";
PostUrlToBaiduResponse response
= BaiduUrlRecorder.Instance.Post(urlTmp);
string baidu = (response != null && response.success == 1 ? "百度推送成功!" : "");

//来源:C/S框架网 | www.csframework.com | QQ:23404761




批量推送n条URL(***URL必须用断行符分开***):



C# Code:

StringBuilder sb = new StringBuilder();

foreach (DataRow R in dt.Rows) sb.AppendLine(R["URL"].ToString());

string urlTmp = sb.ToString();

PostUrlToBaiduResponse response
= BaiduUrlRecorder.Instance.Post(urlTmp);


//来源:C/S框架网 | www.csframework.com | QQ:23404761




百度搜索资源平台




贴图图片-baidu_url_post




C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务



上一篇 下一篇