C#实现百度ping推送功能的方法
网站优化必做的事情之一,百度ping,主动推送给百度
文章添加时调用百度推送方法
//保存
protectedvoidbtnSubmit_Click(objectsender,EventArgse)
{
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());//检查权限
intid=DoAdd();//此id为添加文章返回的此篇文章的id需要修改添加文章的方法返回idif(!(id>0))
{
JscriptMsg("保存过程中发生错误!",string.Empty);
return;
}
//获取频道模板名称
if(channel_id==13||channel_id==16||channel_id==18||channel_id==25||channel_id==6)
{
stringchannelTemp=newBLL.channel().GetModel(this.channel_id).name.ToString();
stringarticleUrl=newBasePage().linkurl(channelTemp+"_show",id);
string[]url=newstring[100];
url[1]="http://www.qishunnet.com"+articleUrl;//例如生成的url如http://www.qishunnet.com/knowledge_show_132.html
stringinfo=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方法
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceDTcms.Common
{
///<summary>
///百度相关类
///</summary>
publicclassBaiDu
{
///<summary>
///推送链接至百度
///</summary>
///<paramname="urls">链接集合</param>
///<returns></returns>
publicstaticstringsendUrlToBaidu(string[]urls)
{
try
{
stringformUrl="http://data.zz.baidu.com/urls?site=www.qishunnet.com&token=FdSmdb3LRa4JitQp";
stringformData="";
foreach(stringurlinurls)
{
formData+=url+"\n";
}
byte[]postData=System.Text.Encoding.UTF8.GetBytes(formData);
//设置提交的相关参数
System.Net.HttpWebRequestrequest=System.Net.WebRequest.Create(formUrl)asSystem.Net.HttpWebRequest;
System.Text.EncodingmyEncoding=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.StreamoutputStream=request.GetRequestStream();
outputStream.Write(postData,0,postData.Length);
outputStream.Close();
System.Net.HttpWebResponseresponse;
System.IO.StreamresponseStream;
System.IO.StreamReaderreader;
stringsrcString;
response=request.GetResponse()asSystem.Net.HttpWebResponse;
responseStream=response.GetResponseStream();
reader=newSystem.IO.StreamReader(responseStream,System.Text.Encoding.GetEncoding("UTF-8"));
srcString=reader.ReadToEnd();
stringresult=srcString;//返回值赋值
reader.Close();
returnresult;
}
catch(Exceptionex)
{
returnex.Message;
}
}
}
}
以上所述是小编给大家介绍的C#实现百度ping推送功能的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!