public voidpostToPing( )
{try{string posturl = “http://ping.baidu.com/ping/RPC2“; //post 提交地址
string refurl = “; //这里可以随便填写.
string content_type = “text/xml“; //提交类型.这里一定要text/xml
string postdt = postdata(); //提交数据
string str = baiduping(posturl, postdt, content_type, refurl, false, Encoding.UTF8);
Stream sm= new System.IO.MemoryStream(Encoding.UTF8.GetBytes(str)); //下面这里检测提交是否成功
XElement xle =XElement.Load(sm);var query = xle.Descendants(“int“);if (query.Count() > 0)
{string _val = query.ElementAtOrDefault(0).Value;if (_val == “1“)
{
Console.WriteLine(“失败“);
}else{
Console.WriteLine(“成功“);
}
}
}catch(Exception ex)
{
Console.WriteLine(ex.Message);//log.Error(ex.Message);
}
}private stringpostdata()
{//注意xml拼接的时候,xml的第一行的开头必须不能有空格等//下面直接是引用百度的例子
StringBuilder sb = newStringBuilder();
sb.AppendLine(“<?xml version=”1.0″ encoding=”UTF-8″?>“);
sb.AppendLine(“weblogUpdates.extendedPing“);
sb.AppendLine(““);
sb.AppendLine(““);
sb.AppendLine(“百度的空间“);
sb.AppendLine(““);
sb.AppendLine(““);
sb.AppendLine(“http://hi.baidu.com/baidu/“);
sb.AppendLine(““);
sb.AppendLine(““);
sb.AppendLine(“http://baidu.com/blog/example.html“);
sb.AppendLine(““);
sb.AppendLine(““);
sb.AppendLine(“http://hi.baidu.com/baidu/rss“);
sb.AppendLine(““);
sb.AppendLine(““);
sb.AppendLine(““);returnsb.ToString().Trim();
}private string baiduping(string targetURL, string formData, string contentType, string referer, boolallowAutoRedirect, Encoding ed)
{
ASCIIEncoding encoding= newASCIIEncoding();byte[] data =encoding.GetBytes(formData);//请求目标网页
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(targetURL);
request.Method= “POST“; //使用post方式发送数据
request.UserAgent = “request“;
request.Referer=referer;
request.ProtocolVersion= new Version(“1.0”); //注意这里这个版本好.一定要设置.现在默认提交是1.1了.否则会一直提示504
request.ContentType= contentType == “” ? “application/x-www-form-urlencoded“: contentType;
request.Timeout= 1000 * 10;
request.ContentLength=data.Length;
Stream newStream=request.GetRequestStream();
newStream.Write(data,0, data.Length);
newStream.Close();
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
Stream stream=response.GetResponseStream();string html = newStreamReader(stream, ed).ReadToEnd();returnhtml;
}