您现在的位置是:网站首页> 编程资料编程资料
ASP.net百度主动推送功能实现代码_实用技巧_
2023-05-24
329人已围观
简介 ASP.net百度主动推送功能实现代码_实用技巧_
官方提供了curl、post、php、ruby的实现示例,并没有C#的官方示例。既然提供了post的方式,那么就可以用C#实现,下面是实现代码:
ASP.net百度主动推送代码范例
public static string PostUrl(string[] urls) { try { string formUrl = "http://data.zz.baidu.com/urls?site=www.yoursite.com&token=yourcode"; string formData = ""; foreach (string url in urls) { formData += url + "\n"; } byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData); // 设置提交的相关参数 System.Net.HttpWebRequest request = System.Net.WebRequest.Create(formUrl) as System.Net.HttpWebRequest; System.Text.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(); System.Net.HttpWebResponse response; System.IO.Stream responseStream; System.IO.StreamReader reader; string srcString; response = request.GetResponse() as System.Net.HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8")); srcString = reader.ReadToEnd(); string result = srcString; //返回值赋值 reader.Close(); return result; } catch(Exception ex) { return ex.Message; } }调用的时候,把您的网址传入
string info = PostUrl(new string[] { "//www.jb51.net/article/1.html", "//www.jb51.net/article/2.html" });
返回的结果是{"remain":498,"success":2} 表示已经推送成功,还剩498条可以推送,本次已经推送成功2条。
另外附上可能出现的异常情况的返回码信息,供调试用:
200 无使用方式错误,需要进一步观察返回的内容是否正确
400 必选参数未提供
405 不支持的请求方式,我们只支持POST方式提交数据
411 HTTP头中缺少Content-Length字段
413 推送的数据过大,超过了10MB的限制
422 HTTP头中Content-Length声明的长度和实际发送的数据长度不一致
500 站长平台服务器内部错误
我实际应用代码
//点击按钮触发 protected void Button1_Click(object sender, EventArgs e) { string info = PostUrl(new string[] { "//www.jb51.net/", "//www.jb51.net/article/3.html" }); this.Label1.Text= info; }到此这篇关于ASP.net百度主动推送功能实现代码的文章就介绍到这了,更多相关ASP.net百度主动推送内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- asp .net core静态文件资源的深入讲解_实用技巧_
- .net core实用技巧——将EF Core生成的SQL语句显示在控制台中_实用技巧_
- 使用Seq搭建免费的日志服务的方法_实用技巧_
- asp.net core应用docke部署到centos7的全过程_实用技巧_
- ASP.Net中的async+await异步编程的实现_实用技巧_
- ASP.NET Core根据环境变量支持多个 appsettings.json配置文件_实用技巧_
- Linux安装.Net core 环境并运行项目的方法_实用技巧_
- 如何在ASP.Net Core中使用 IHostedService的方法_实用技巧_
- ASP.NET Core中的配置详解_实用技巧_
- .NET使用DinkToPdf将HTML转成PDF的示例代码_实用技巧_
