C#调用WordPress的xmlrpc.php远程发布文章
2011-08-05 11:42 iLoae 阅读(1675) 评论(0) 编辑 收藏 举报原文地址:http://www.opencn.info/archives/226.html
很多博客系统都支持xmlrpc,这是一套基于xml的远程发布协议。
C#调用起来很简单,超简单,不费吹灰之力就可以完成。
在观看高潮的代码之前的准备一定的前戏是不可或缺的。
首先要打开并仔细阅读一下: xml-rpc API的中文文档与说明
其次这是关于WordPress的开发,还需要准备WordPress的xmlrpc的文档,不过是英文的:xml-rpc wp 相关API。
看过之后,你就会明白,为什么这里用的是metaweblog的API.
下面就是高潮了View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using CookComputing.XmlRpc;
6
7
8 /// <summary>
9 /// 定义文章结构
10 /// </summary>
11 [XmlRpcMissingMapping(MappingAction.Ignore)]
12 public struct Post
13 {
14 public DateTime dateCreated;
15 /// <summary>
16 /// 文章正文内容
17 /// </summary>
18 public string description;
19 public string title;
20 public string postid;
21 /// <summary>
22 /// 分组名称即可
23 /// </summary>
24 public string[] categories;
25 /// <summary>
26 /// 1是可以评论,0是邪恶的关平
27 /// </summary>
28 public int mt_allow_comments;
29 public int mt_allow_pings;
30 public int mt_convert_breaks;
31 public string mt_text_more;
32 /// <summary>
33 /// 文章摘要
34 /// </summary>
35 public string mt_excerpt;
36 /// <summary>
37 /// Tags,也叫标签
38 /// </summary>
39 public string mt_keywords;
40 //public int[] mt_tb_ping_urls;
41 }
42
43 public class MetaBlogAPI : XmlRpcClientProtocol
44 {
45 [XmlRpcMethod("metaWeblog.newPost")]
46 public string newPost(string blogid, string username,
47 string password, Post content,bool publish)
48 {
49 return (string)this.Invoke
50 ("newPost",
51 new object[] { blogid, username, password, content, publish });
52 }
53
54 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using CookComputing.XmlRpc;
6
7
8 /// <summary>
9 /// 定义文章结构
10 /// </summary>
11 [XmlRpcMissingMapping(MappingAction.Ignore)]
12 public struct Post
13 {
14 public DateTime dateCreated;
15 /// <summary>
16 /// 文章正文内容
17 /// </summary>
18 public string description;
19 public string title;
20 public string postid;
21 /// <summary>
22 /// 分组名称即可
23 /// </summary>
24 public string[] categories;
25 /// <summary>
26 /// 1是可以评论,0是邪恶的关平
27 /// </summary>
28 public int mt_allow_comments;
29 public int mt_allow_pings;
30 public int mt_convert_breaks;
31 public string mt_text_more;
32 /// <summary>
33 /// 文章摘要
34 /// </summary>
35 public string mt_excerpt;
36 /// <summary>
37 /// Tags,也叫标签
38 /// </summary>
39 public string mt_keywords;
40 //public int[] mt_tb_ping_urls;
41 }
42
43 public class MetaBlogAPI : XmlRpcClientProtocol
44 {
45 [XmlRpcMethod("metaWeblog.newPost")]
46 public string newPost(string blogid, string username,
47 string password, Post content,bool publish)
48 {
49 return (string)this.Invoke
50 ("newPost",
51 new object[] { blogid, username, password, content, publish });
52 }
53
54 }
Attribute XmlRpcMissingMapping 是用来标记这个结构的所有成员都是可选的(XML-RPC这套东西每个博客都不尽相同,而且也很难确定到底会返回什么东西)
调用的时候没啥特别的,继续看:
View Code
1 private void button1_Click(object sender, EventArgs e)
2 {
3 MetaBlogAPI mba = new MetaBlogAPI();
4 mba.Url = "http://你的博客地址/xmlrpc.php";
5 Post newpost = new Post();
6 newpost.title = "这是一篇用测试MetaAPI的测试内容";
7 newpost.dateCreated = DateTime.Now;
8 newpost.description = "这篇文章是关于C#如何发布Wordpress一切细节的开始,同样也是一切的结束";
9 newpost.mt_keywords = "Wordpress,应用技术";
10 newpost.categories = new string[] {"C#"};
11
12 string a = mba.newPost("blog_id", "你的用户名", "你的密码", newpost, true);
13 MessageBox.Show(a);
14 }
2 {
3 MetaBlogAPI mba = new MetaBlogAPI();
4 mba.Url = "http://你的博客地址/xmlrpc.php";
5 Post newpost = new Post();
6 newpost.title = "这是一篇用测试MetaAPI的测试内容";
7 newpost.dateCreated = DateTime.Now;
8 newpost.description = "这篇文章是关于C#如何发布Wordpress一切细节的开始,同样也是一切的结束";
9 newpost.mt_keywords = "Wordpress,应用技术";
10 newpost.categories = new string[] {"C#"};
11
12 string a = mba.newPost("blog_id", "你的用户名", "你的密码", newpost, true);
13 MessageBox.Show(a);
14 }
找不到System.web模块的解决方法
一定要在项目中添加System.web的引用
下载XML-RPC.NET
下载之后在bin里找到CookComputing.XmlRpcV2.dll,添加为项目的引用