Create a metaweblog API in ASP. NET

Source: Internet
Author: User

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: blog user ID, name, last name or email.

Post: indicates the blog post, title, body, and category.

Categoryinfo: the type, number, and name of the blog.

Mediaobject: the name, type, and data of the media object (image, audio, 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 new post.

Metaweblog. editpost: updates the post.

Metaweblog. getcategories: gets the blog category.

Metaweblog. getpost: Get a single post data.

Metaweblog. getrecentposts: the most recent post.

Metaweblog. newmediaobject: adds a new media object.

Blogger. deletepost: delete a post.

Blogger. getuserinfo: obtains user information.

Blogger. getusersblogs: Get the user's blog list.

How to Create metaweblog

1. First download the XML-RPC.NET and 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, see the metaweblog API specifications.

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

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 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 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 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 info = 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 successful. 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.

Attachment: metaweblogsample.zip: http://file.ddvip.com/2008_10/1224235867_ddvip_8763.zip

Org: http://www.sucai.com/Tech/List4/7428.htm

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.