C#实现百度ping推送功能的方法
程序员文章站
2023-08-26 23:45:17
网站优化必做的事情之一,百度ping,主动推送给百度
文章添加时调用百度推送方法
//保存
protected void btnsubmit_clic...
网站优化必做的事情之一,百度ping,主动推送给百度
文章添加时调用百度推送方法
//保存 protected void btnsubmit_click(object sender, eventargs e) { if (action == dtenums.actionenum.edit.tostring()) //修改 { chkadminlevel("channel_" + this.channel_name + "_list", dtenums.actionenum.edit.tostring()); //检查权限 if (!doedit(this.id)) { jscriptmsg("保存过程中发生错误啦!", string.empty); return; } jscriptmsg("修改信息成功!", "article_list.aspx?channel_id=" + this.channel_id); } else //添加 { chkadminlevel("channel_" + this.channel_name + "_list", dtenums.actionenum.add.tostring()); //检查权限 int id=doadd(); //此 id为添加文章返回的 此篇文章的id 需要修改添加文章的方法 返回id if (!(id>0)) { jscriptmsg("保存过程中发生错误!", string.empty); return; } //获取频道模板名称 if (channel_id == 13 || channel_id == 16 || channel_id == 18 || channel_id == 25 || channel_id == 6) { string channeltemp = new bll.channel().getmodel(this.channel_id).name.tostring(); string articleurl = new basepage().linkurl(channeltemp + "_show", id); string[] url = new string[100]; url[1] = "http://www.qishunnet.com" + articleurl;//例如生成的url如http://www.qishunnet.com/knowledge_show_132.html string info = dtcms.common.baidu.sendurltobaidu(url); jscriptmsg("添加信息成功!", "article_list.aspx?channel_id=" + this.channel_id + "&message=添加信息成功!百度推送返回信息=" + info + "url:" + url[1]); return; } jscriptmsg("添加信息成功!", "article_list.aspx?channel_id=" + this.channel_id); } }
common类库增加百度ping方法
using system; using system.collections.generic; using system.linq; using system.text; namespace dtcms.common { /// <summary> /// 百度相关类 /// </summary> public class baidu { /// <summary> /// 推送链接至百度 /// </summary> /// <param name="urls">链接集合</param> /// <returns></returns> public static string sendurltobaidu(string[] urls) { try { string formurl = " http://data.zz.baidu.com/urls?site=www.qishunnet.com&token=fdsmdb3lra4jitqp"; 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; } } } }
以上所述是小编给大家介绍的c#实现百度ping推送功能的方法,希望对大家有所帮助