XML-RPC in C #

Source: Internet
Author: User

To operate WordPress, in addition to its Web background, there is also a way to achieve through XML-RPC.
I will not repeat the XML-RPC, Simply put: the XML containing the operational commands and operational data is transmitted over Http TO THE XML-RPC Server. The Server then performs operations based on the commands and data analyzed in the XML, and then returns the user's XML, which contains the XML of the user's required data.

Take Wordpress's wp. getUsersBlogs command as an example.

Wp. getUsersBlogs
Retrieve the blogs of the users.
Parameters

  • String username
  • String password

Return Values

  • Array

    • Struct

      • Boolean is_admin
      • String url
      • Int blog_id
      • String blog_name
      • String xmlrpc_url

The above is_admin indicates whether the user is an administrator. The url is of course your blog address, and blog_id indicates the blog id to be used later, blog_name refers to the blog name (for example, my "fengge") and xmlrpc_url is the address of the XML-RPC Server.
It is worth mentioning that for Wordpress, if your blog address is a http://www.xxx.com, his XML-RPC Server address is http://wwwxxx.com/xmlrpc.php.
Its input XML is

<methodCall>    <methodName>wp.getUsersBlogs</methodName>    <params>        <param>            <value>                Username            </value>        </param>        <param>            <value>                Password            </value>        </param>    </params></methodCall>

The above Username is the user name, And the Password is your Password. wp. getUsersBlogs is the operation name for obtaining the Blog.
The returned XML is (take me as an example) when it is correct)

<MethodResponse> <params> <param> <value> <array> <data> <value> <struct> <member> <name> isAdmin </name> <value> <boolean> 1 </boolean> </value> </member> <name> url </name> <value> <string> http://www.iron-feet.cn/</string> </value> </member> <name> blogid </name> <value> <string> 1 </string> </value> </member> <name> blogName </name> <value> <string> fengyunge </string> </value> </member> <name> xmlrpc </name> <value> <string> http://www.iron-feet.cn/xmlrpc.php </string> </value> </member> </struct> </value> </data> </array> </value> </param> </params> </methodResponse>

The XML format is

<methodResponse>  <fault>    <value>      <struct>        <member>          <name>faultCode</name>          <value>            <int>403</int>          </value>        </member>        <member>          <name>faultString</name>          <value>            <string>Bad login/pass combination.</string>          </value>        </member>      </struct>    </value>  </fault></methodResponse>

Based on the official Wordpress website, Wordpress also supports the Blogger API, MetaWeblog API, and Movable Type API

WordPress uses an XML-RPC interface. We currently supportBlogger API, MetaWeblog API, and the Movable Type API.

You can use relevant documents and the structure above to obtain the correct XML

Since the advent of Linq, many things have become very convenient, for example, constructing XML. Next, we will introduce how to use the C # language to construct XML through Linq,
For example, the constructed code of the wp. getUsersBlogs XML file is as follows:

                    XElement XmethodCall=                        new XElement("methodCall",                            new XElement("methodName", "wp.getUsersBlogs"),                            new XElement("params",                                new XElement("param",                                    new XElement("value",                                        new XElement("string", _strUsr)                                    )                                ),                                new XElement("param",                                    new XElement("value",                                        new XElement("string", _strPwd)                                    )                                )                            )                        );

The method for sending a Post via C # is as follows:

XElement Post (XElement XmethodCall) {WebRequest req = WebRequest. create (strXmlRpc); // strXmlRpc is the address of the XML-RPC Server, for example: Mine is the http://www.iron-feet.cn/xmlrpc.php req. contentType = "application/x-www-form-urlencoded"; req. method = "POST"; byte [] bytes = System. text. encoding. ASCII. getBytes (XmethodCall. toString (); req. contentLength = bytes. length; System. IO. stream OS = req. getRequestStream (); OS. write (byte S, 0, bytes. length); OS. close (); System. net. webResponse resp = req. getResponse (); System. IO. streamReader sr = new System. IO. streamReader (resp. getResponseStream (); XElement tempX = XElement. parse (sr. readToEnd (). trim (); var query = tempX. elements ("fault "). elements ("value "). elements ("struct "). elements ("member "). elements ("name"); foreach (XElement x in query) {if (x. value = "faultString") {throw ne W Exception (x. parent. elements ("value "). first (). value) ;}} if (tempX. name. toString () = "methodResponse") {return tempX;} else {throw new Exception ("This is not an XML-RPC server! ");}}

The introduction is complete.

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.