2018-05-29  1,218 views 4

ASP.NET/ASP.NET MVC 百度SEO 链接自动提交 熊掌号自动提交实现

百度站长平台为了方便站长对自己网站内容的提交(新数据、历史数据),提供了链接提交API,百度熊掌号也是这样的,但是官网给出的范例是使用curl,post,php的方式。而对于使用ASP.NET开发网站的站长来说并没有文档。其实众所周知,C#是可以模拟Post提交的,因此解决方案也就产生了。

一、对官网提供的API分析:

1、百度站长链接自动提交(Post):

API地址:http://data.zz.baidu.com/urls?site=*****&token=*****

参数:多条需要提交的Url 换行即可

返回参数:

状态码为200,可能返回以下字段:


成功返回示例:

{

    "remain":4999998,
    "success":2,
    "not_same_site":[],
    "not_valid":[]
}

2、熊掌号资源自动提交(Post)

API地址:http://data.zz.baidu.com/urls?appid=*******&token=*******&type=batch

参数:type  batch (历史数据) realtime(新数据)

参数:多条需要提交的Url 换行即可

返回参数:

状态码为200,可能返回以下字段:


成功返回示例:

{
"remain_realtime": 9,
"success_realtime": 1
}

二、参考官方提供的Post请求方式,尝试对百度SEO链接自动提交 (Demo) 代码如下:

前端:

@{
    ViewBag.Title = "ASP.NET 模拟Post 请求API 自动推送网站内容给百度";
}

<form action="/PushContent/Push" method="post">
    <div style="border:1px solid #ccc; padding:10px; width:460px;margin:10px; height:100px;">@ViewBag.Result</div>
    <input type="submit" value="提交数据" />
</form>

后端:

/// <summary>
/// 提交数据
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult Push()
{
    string apiUrl = @"http://data.zz.baidu.com/urls?site=*******&token=*******";
    string formData = "";
    //TODO
    //查询要提交的Url集合
    List<string> urlList = new List<string> { 
        "http://www.51softs.com/sql-server/sql-server-use-sqlfile-import-database.html"
    };

    //循环Url集合 拼接成需要提交的格式 (\n 换行即可)
    foreach (var url in urlList)
    {
        formData += url + "\n";
    }
    byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData);
    //模拟Post 并设置Http请求标头
    HttpWebRequest request = System.Net.WebRequest.Create(apiUrl) as System.Net.HttpWebRequest;
    Encoding myEncoding = System.Text.Encoding.UTF8;
    request.Method = "POST";
    request.KeepAlive = false;
    request.AllowAutoRedirect = true;
    request.ContentType = "text/plain";
    request.UserAgent = "curl/7.12.1";
    request.ContentLength = postData.Length;
    //模拟请求并获取响应值
    System.IO.Stream outputStream = request.GetRequestStream();
    outputStream.Write(postData, 0, postData.Length);
    outputStream.Close();
    HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8"));
    string resultData = reader.ReadToEnd();
    ViewBag.Result = resultData;
    return View("Index");
}

实现效果如下:

 

因此可以提取以下工具类代码:

public static class BaiduPush
{
    /// <summary>
    /// 百度站长SEO链接自动提交、熊掌号链接提交 通用方法
    /// </summary>
    /// <param name="urlList">需要提交的链接地址 新增数据只需要一条  若是提交历史通常从数据库查询出来</param>
    /// <param name="api">百度站长工具提供的API 地址</param>
    /// <returns></returns>
    public static string BaiduUrlPush(List<string> urlList,string api)
    {
        string formData = "";
        foreach (var url in urlList)
        {
            formData += url + "\n";
        }
        byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData);
        //模拟Post 并设置Http请求标头
        HttpWebRequest request = System.Net.WebRequest.Create(api) as System.Net.HttpWebRequest;
        Encoding myEncoding = System.Text.Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.ContentType = "text/plain";
        request.UserAgent = "curl/7.12.1";
        request.ContentLength = postData.Length;
        //模拟请求并获取响应值
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();
        HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8"));
        string resultData = reader.ReadToEnd();
        return resultData;
    }
}

			
							
													
		

4 条留言  访客:2 条  博主:2 条

  1. beyond

    ASP.NET 链接自动提交 熊掌号自动提交实现
    请问一下 动易sf系统可以实现自动提交熊掌号吗?怎么实现

  2. beyond

    ASP.NET 动易sf可以实现百度熊掌号吗,可以帮我实现的话,可以付费也可以的哦,在你们网站说找不到联系方式,麻烦加一下我QQ:410390469 谢谢

    • 51softs 博主

      @beyond 好的 加了

    • 51softs 博主

      @beyond 不知道你用的是源码版还是编译版本,如果是编译版本的话建议做一个插件

给我留言

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: