ASP. NET create metaweblog API

Source: Internet
Author: User

There is no doubt that the metaweblog API is a very useful and popular invention, only for some general services of blogs and other websites.

 

Concept

Metaweblog is an XML-based RPC communication (download ). This means that you have a set of pre-defined structures (simple data type attributes) that are being transferred between the client and the server.

You need to use the following six structures of the metaweblog API:

    • Bloginfo:The URL, ID, or name of the blog.
    • Userinfo:The blog user's ID, name, last name, or email.
    • Post:It is a blog post, title, body, and category.
    • Categoryinfo:Blog category information, number, and name.
    • Mediaobject:Names, types, and data about media objects (images, audios, and other file types.
    • Mediaobjectinfo:Media object.

 

As a general rule, you can remember that the metaweblog API uses the string type as the basic type, parameters, return types, and no integer type. The Boolean and base64 encoding strings are also used in several places.

 

The metaweblog API has nine methods:

    • Metaweblog. newpost:Add a newPost.
    • Metaweblog. editpost:Update the post.
    • Metaweblog. getcategories:Obtain the blog category.
    • Metaweblog. getpost:Obtain a single post data.
    • Metaweblog. getrecentposts:The most recent post.
    • Metaweblog. newmediaobject:Add a new media object.
    • Blogger. deletepost:Delete a post.
    • Blogger. getuserinfo:Obtain user information.
    • Blogger. getusersblogs:Obtain the user's blog list.

 

How to Create metaweblog

1. DownloadXML-RPC.NETAnd then add the reference to the project ..

 

 

2. Create an HTTP handler or WebService. HTTP processing is created here.ProgramMetaweblogapi. ashx. And set the entry point-class = "metaweblogsample. metaweblog ".

 
<% @ Webhandler Language = "C #" codebehind = "metaweblogapi. ashx. cs" class = "metaweblogsample. metaweblog" %>
 
 
 
 

3. Create structures (structs. CS). For how to correctly create this structure, seeMetaweblog APISpecification. 

The followingCodeIs the structure I created. You can also use the same code in your project, because these structures are fixed.

Using System; Using System. Data; Using System. configuration; Using System. LINQ; Using System. Web; Using System. Web. Security; Using System. Web. UI; Using System. Web. UI. htmlcontrols; Using System. Web. UI. webcontrols; Using System. Web. UI. webcontrols. webparts; Using System. xml. LINQ; Using Cookcomputing. XMLRPC;Namespace Metaweblogsample { # Region Structs Public   Struct Bloginfo { Public   String Blogid; Public   String URL; Public   String Blogname ;} Public   Struct CATEGORY { Public   String Categoryid; Public   String Categoryname;} [serializable]Public   Struct Categoryinfo { Public   String Description; Public   String Htmlurl; Public   String Rssurl; Public   String Title; Public   String Categoryid;} [xmlrpcmissingmapping (mappingaction. Ignore)] Public   Struct Enclosure { Public   Int Length;Public   String Type; Public   String URL;} [xmlrpcmissingmapping (mappingaction. Ignore)] Public   Struct Post { Public Datetime datecreated; Public   String Description; Public   String Title; Public   String [] Categories; Public   String Permalink; Public  Object Postid; Public   String Userid; Public   String Wp_slug;} [xmlrpcmissingmapping (mappingaction. Ignore)] Public   Struct Source { Public   String Name; Public   String URL ;} Public   Struct Userinfo { Public   String Userid; Public  String Firstname; Public   String Lastname; Public   String Nickname; Public   String Email; Public   String URL;} [xmlrpcmissingmapping (mappingaction. Ignore)] Public   Struct Mediaobject { Public   String Name; Public   String Type; Public  Byte [] Bits;} [serializable] Public   Struct Mediaobjectinfo { Public   String URL ;} # Endregion }
 
4. Create the metaweblog API interface (imetaweblog. CS ). The definition of this interface is also the metaweblog specification. There are two groups of core metaweblog APIs and blogger APIs. The Code is as follows:
 Using System; Using System. Data; Using System. configuration; Using System. LINQ; Using System. Web; Using System. Web. Security; Using System. Web. UI; Using System. Web. UI. htmlcontrols; Using System. Web. UI. webcontrols; Using System. Web. UI. webcontrols. webparts; Using System. xml. LINQ; Using Cookcomputing. XMLRPC; Namespace Metaweblogsample { Public   Interface Imetaweblog { # Region Metaweblog API [xmlrpcmethod ( "Metaweblog. newpost" )] String Addpost (String Blogid, String Username, String Password, post, Bool Publish); [xmlrpcmethod ( "Metaweblog. editpost" )] Bool Updatepost ( String Postid, String Username, String Password, post, Bool Publish); [xmlrpcmethod ( "Metaweblog. getpost" )] Post getpost ( String Postid, String Username,String Password); [xmlrpcmethod ( "Metaweblog. getcategories" )] Categoryinfo [] getcategories ( String Blogid, String Username, String Password); [xmlrpcmethod ( "Metaweblog. getrecentposts" )] Post [] getrecentposts ( String Blogid, String Username, String Password, Int Numberofposts); [xmlrpcmethod ( "Metaweblog. newmediaobject" )] Mediaobjectinfo newmediaobject ( String Blogid, String Username, String Password, mediaobject ); # Endregion  # Region Blogger API [xmlrpcmethod ( "Blogger. deletepost" )] [ Return : Xmlrpcreturnvalue (description = "Returns true ." )] Bool Deletepost ( String Key, String Postid, String Username, String Password, Bool Publish); [xmlrpcmethod ( "Blogger. getusersblogs" )] Bloginfo [] getusersblogs ( String Key, String Username, String Password); [xmlrpcmethod ( "Blogger. getuserinfo" )] Userinfo getuserinfo ( String Key, String Username, String Password ); # Endregion }}
 
5. It is also the last step to implement the interface .. In addition, you also need a method to verify the user's username and password. The method that allows the user to implement the interface can be verified. The following code varies depending on your blog engine or website.
 Using System; Using System. Data; Using System. configuration; Using System. LINQ; Using System. Web; Using System. Web. Security; Using System. Web. UI; Using System. Web. UI. htmlcontrols; Using System. Web. UI. webcontrols; Using System. Web. UI. webcontrols. webparts; Using System. xml. LINQ; Using Cookcomputing. XMLRPC; Using System. Collections. Generic; /// /// Note that the source is from the network ..  ///  Namespace Metaweblogsample { Public   Class Metaweblog: xmlrpcservice, imetaweblog { # Region Public Constructors Public Metaweblog (){} # Endregion  # Region Imetaweblog members String Imetaweblog. addpost ( String Blogid, String Username, String Password, post, Bool Publish ){If (Validateuser (username, password )){ String Id = String . Empty; // Todo: return a string based on the actual situation, generally the blog ID.  Return ID ;} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Bool Imetaweblog. updatepost ( String Postid, String Username, String Password, post, Bool Publish ){ If (Validateuser (username, password )){ Bool Result = False ; // Todo: return a Boolean value based on the actual situation, indicating whether the update is successful.  Return Result ;} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Post imetaweblog. getpost ( String Postid, String Username, String Password ){ If (Validateuser (username, password) {post = New Post ();// Todo: return a struct {struct is a standard format,  // The format is the post attribute. Note that category is an array and is the category of the post.  // If the category does not exist, the server only processes the existing category }.  Return Post ;} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Categoryinfo [] imetaweblog. getcategories ( String Blogid, String Username, String Password ){ If (Validateuser (username, password) {list <categoryinfo> categoryinfos = New List <categoryinfo> ();// Todo: Obtain the blog category based on the actual situation and set categoryinfo.  Return Categoryinfos. toarray ();} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Post [] imetaweblog. getrecentposts ( String Blogid, String Username, String Password, Int Numberofposts ){ If (Validateuser (username, password) {list <post> posts = New List <post> (); // Todo: returns an array of a structure (struct ). // Each struct contains a structure with the same getpost return value. Set and return.  Return Posts. toarray ();} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Mediaobjectinfo imetaweblog. newmediaobject ( String Blogid, String Username, String Password, mediaobject ){ If (Validateuser (username, password) {mediaobjectinfo objectinfo = New Mediaobjectinfo (); // Todo: returns an array. // Blogid, username, and password represent the blog ID (Note: if you have two blogs, blogid specifies the blog you want to edit), user name, and password.  // Struct must contain three elements: name, type, and bits. Of course, it can also contain other elements.  Return Objectinfo ;} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Bool Imetaweblog. deletepost ( String Key, String Postid, String Username, String Password, Bool Publish ){ If (Validateuser (username, password )){ Bool Result = False ; // Todo: return a Boolean value based on the actual situation, indicating whether the deletion is successful.  Return Result ;} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Bloginfo [] imetaweblog. getusersblogs ( String Key, String Username, String Password ){ If (Validateuser (username, password) {list <bloginfo> infolist = New List <bloginfo> ();// Todo: Obtain the current user's blog information based on the actual situation and set the user's blog information.  Return Infolist. toarray ();} Throw   New Xmlrpcfaultexception (0, "User is not valid! " );} Userinfo imetaweblog. getuserinfo ( String Key, String Username, String Password ){ If (Validateuser (username, password) {userinfo = New Userinfo (); // Todo: Obtain the current user information based on the actual situation and set the user information.  Return Info ;} Throw  New Xmlrpcfaultexception (0, "User is not valid! " );} # Endregion  # Region Private methods Private   Bool Validateuser ( String Username, String Password ){ Bool Result = False ; // Todo: implement the logic to validate the user  Return Result ;} # Endregion }}
 
 

6. The compilation is passed. test:HTTP: /localhost: 1269/metaweblogapi. ashx

 

 

The test is passed. I have to study how to use it on my blog. It is worth noting that the http://www.xmlrpc.com package downloaded through the xml-rpc.net has included the code for each structure, interface and method, Please study on your own.

Download [62kb]

 

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.