C#主动POST推送URL链接到百度站长平台(支持提交、更新和删除URL链接)
作者:C/S框架网|www.cscode.ne  发布日期:2020-03-07 20:04:34
  C#主动POST推送URL链接到百度站长平台(支持提交、更新和删除URL链接)

C#主动推送URL链接到百度站长平台(支持提交、更新和删除URL链接)

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

贴图图片-主动推送URL链接到百度站长平台


C# Code:

/// <summary>
/// C#推送链接URL到百度搜索资源平台提高收录量
/// </summary>
public class PostUrlToBaidu
{
  
/// <summary>
  
/// 百度推送接口调用地址
  
/// </summary>
  
private const string api_url_post = "http://data.zz.baidu.com/urls?site=www.csframework.com&token=T29eSA53UHSvJI4zq";
  
private const string api_url_del = "http://data.zz.baidu.com/urls?site=www.csframework.com&token=T29eSA53UHSvJI4zq";
  
private const string api_url_update = "http://data.zz.baidu.com/urls?site=www.csframework.com&token=T29eSA53UHSvJI4zq";
  
  
/// <summary>
  
/// 推送链接URL到百度搜索资源平台
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public static PostUrlToBaiduResponse Post(string url)
  {
    
return PostUrlToBaidu.PostUrl(api_url_post, url);
  }
  
  
/// <summary>
  
/// 更新链接url
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public static PostUrlToBaiduResponse Update(string url)
  {
    
return PostUrlToBaidu.PostUrl(api_url_update, url);
  }
  
  
/// <summary>
  
/// 删除链接url
  
/// </summary>
  
/// <param name="url"></param>
  
/// <returns></returns>
  
public static PostUrlToBaiduResponse Delete(string url)
  {
    
return PostUrlToBaidu.PostUrl(api_url_del, url);
  }
  
  
/// <summary>
  
/// POST方式调用百度站长平台(资源搜索平台)API接口
  
/// </summary>
  
/// <param name="baidu_api_url">百度API接口</param>
  
/// <param name="my_url">URL地址</param>
  
/// <returns></returns>
  
private static 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



测试案例:


贴图图片-主动推送URL链接到百度站长平台1


贴图图片-主动推送URL链接到百度站长平台2





扫一扫加微信:
 
上一篇 下一篇