C # automatically submit to Baidu ping Service

Source: Internet
Author: User

I have no intention of seeing this ping service in Baidu webmaster today )....

So what is Ping service?

Ping is an update Notification Service Based on the XML_RPC standard protocol. It is used by a blog to quickly notify Baidu of content updates, so that Baidu can capture and update content in a timely manner.

It looks like a simple http post submission. However, when you use WebRequest to simulate the request, the return operation times out. It's speechless.

The following is an example of the Baidu ping Service (note that the red part. Now the http Version is 1.1. This is the details that make it unable to be submitted). By the way, the Fiddler tool is really useful.

 

WeblogUpdates. extendedPing xml-rpc Request example:
POST/ping/RPC2 HTTP/1.0
User-Agent: request
Host: ping.baidu.com
Content-Type: text/xml
Content-Length: 511

<? Xml version = "1.0" encoding = "UTF-8"?> <MethodCall>
<MethodName> weblogUpdates. extendedPing </methodName>
<Params>
<Param>
<Value> <string> Baidu space </string> </value>
</Param>
<Param>
<Value> <string> http://hi.baidu.com/baidu/ </string> </value>
</Param>
<Param>
<Value> <string> http://baidu.com/blog/example.html </string> </value>
</Param>
<Param>
<Value> <string> http://hi.baidu.com/baidu/rss </string> </value>
</Param>
</Params>
</MethodCall> below is a simple example of my own (tested)


Public void postToPing ()
{
Try
{
String posturl = "http://ping.baidu.com/ping/RPC2"; // post submission address
String refurl = "http://www.weletgo.com/"; // you can enter it here.
String content_type = "text/xml"; // submission type. text/xml is required here.
String postdt = postdata (); // submit data
String str = baiduping (posturl, postdt, content_type, refurl, false, Encoding. UTF8 );
Stream sm = new System. IO. MemoryStream (Encoding. UTF8.GetBytes (str); // check whether the submission is successful.
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 ("failed ");
}
Else
{
Console. WriteLine ("succeeded ");
}
}
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );

// Log. Error (ex. Message );
}
}
Private string postdata ()
{
// Note that when splicing xml, the first line of xml must start with a space, and so on. // The following example directly references Baidu.
StringBuilder sb = new StringBuilder ();
Sb. AppendLine ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <MethodCall> ");
Sb. AppendLine ("<methodName> weblogUpdates. extendedPing </methodName> ");
Sb. AppendLine ("<params> ");
Sb. AppendLine ("<param> ");
Sb. AppendLine ("<value> <string> Baidu space </string> </value> ");
Sb. AppendLine ("</param> ");
Sb. AppendLine ("<param> ");
Sb. AppendLine ("<value> <string> http://hi.baidu.com/baidu/ </string> </value> ");
Sb. AppendLine ("</param> ");
Sb. AppendLine ("<param> ");
Sb. AppendLine ("<value> <string> http://baidu.com/blog/example.html </string> </value> ");
Sb. AppendLine ("</param> ");
Sb. AppendLine ("<param> ");
Sb. AppendLine ("<value> <string> http://hi.baidu.com/baidu/rss </string> </value> ");
Sb. AppendLine ("</param> ");
Sb. AppendLine ("</params> ");
Sb. AppendLine ("</methodCall> ");
Return sb. ToString (). Trim ();
}

Private string baiduping (string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect, Encoding ed)
{
ASCIIEncoding encoding = new ASCIIEncoding ();
Byte [] data = encoding. GetBytes (formData );
// Request the target webpage
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (targetURL );
Request. Method = "POST"; // use post to send data
Request. UserAgent = "request ";
Request. Referer = referer;
Request. ProtocolVersion = new Version ("1.0"); // note that this Version is good. You must set it. The default submission is 1.1. Otherwise, the system will always prompt 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 = new StreamReader (stream, ed). ReadToEnd ();
Return html;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.