Asp.net中C#調用xml-rpc遠程發布文章

來源:互聯網
上載者:User

XML-RPC是一個遠端程序呼叫(遠端程式呼叫)(remote procedure call,RPC)的分散式運算協議,通過XML將調用函數封裝,並使用HTTP協議作為傳送機制
天說的是wordpress的xmlrpc介面,是c#版本的;用到了兩個庫xml-rpc.net和JoeBlogs。
xml-rpc.net是一個 .NET 的用戶端伺服器的基於 XML-RPC 遠端程序呼叫的架構。

 代碼如下 複製代碼


[XmlRpcUrl("http://www.111cn.net /RPC2")]
public interface IStateName : IXmlRpcProxy
{
    [XmlRpcMethod("examples.getStateName")]
    string GetStateName(int stateNumber);
}
IStateName proxy = XmlRpcProxyGen.Create();
string stateName = proxy.GetStateName(41);

看了一下,這個架構本身就支援BloggerAPI、MeerkatAPI、MetaWeblogAPI、MovableTypeAPI這幾個現成的介面,wordpress也都支援這幾個API函數。
不過,萬惡的拿來主義驅使我尋找更好的API封裝,JoeBlogs這個針對wordpress的API庫是個不錯的選擇。

 

 代碼如下 複製代碼
string Url = "www.youblogs.com";
string User = "MyUser"; //enter your username
string Password = "MyPassword"; //enter your password
 
var wp = new WordPressWrapper(Url, User, Password);
 
//a few test functions...
var userBlogs = wp.GetUserBlogs();
var tags = wp.GetTags();
var categories = wp.GetCategories();

 
var authors = wp.GetAuthors();
不過這個庫也需要小小的改造,本身有好多介面都沒有實現,需要自己編寫一部分代碼,這點需要注意了^_^。
開發過程中發現,使用Joe Blogs發布部落格時無法設定分類,對比live writer 發布部落格時http包發現,wordpress設定分類使用的分類名,而不是分類Id;

OK,找到原因後,修改Joe Blogs源碼:

1. 類 JoeBlogs.Post 修改為

 代碼如下 複製代碼

using System;
namespace JoeBlogs
{
    public class Post
    {
        private int _postID;
        public Post()
        {
            DateCreated = DateTime
                .Now;
        }
        public int PostID { get { return _postID; } }
        public DateTime DateCreated { get; set; }
        public string Body { get; set; }
        public string Title { get; set; }
        //public int[] Categories { get; set; }
        public string[] Categories { get; set; }
        public string[] Tags { get; set; }
        public override string ToString()
        {
            return this.Body;
        }
    }
}

2. 結構體 JoeBlogs.XmlRpc.XmlRpcPost 修改為

 代碼如下 複製代碼

using System;
using CookComputing.XmlRpc;
namespace JoeBlogs.XmlRpc
{
    /// <summary>
    /// This struct represents the information about a post that could be returned by the
    /// EditPost(), GetRecentPosts() and GetPost() methods.
    /// </summary>
    [XmlRpcMissingMapping(MappingAction.Ignore)]
    public struct XmlRpcPost
    {
        public DateTime dateCreated;
        public string description;
        public string title;
        public string postid;
        //public int[] categories;
        public string[] categories;
        public string[] mt_keywords;
        public override string ToString()
        {
            return this.description;
        }
    }
}

3. 建立文章時,將分類名傳給文章的分類

 代碼如下 複製代碼
Post post = new Post();
post.Title = title;
post.Body = body;
post.Categories = new string[] { category.Name };

一切搞定!

範例程式碼:

 代碼如下 複製代碼

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string XmlRpcUrl = "http://www.111cn.net /xmlrpc.php";
        public string Username = "liuweihug";
        public string Password = "65linyi!!";
       
     
        JoeBlogs.IWordPressWrapper _wpWrapper;
        //LoginInfo LoginInfo = new LoginInfo();
        private int Postdoc()
        {
            var _wpWrapper = new WordPressWrapper(XmlRpcUrl, Username, Password);
               
            var post = new Post()
            {
                Body = this.richTextBox1.Text,
                Categories = new string[] { textBox2.Text },
                Tags = new string[] { "測試" },
                Title = this.textBox1.Text,
                
            };
            return _wpWrapper.NewPost(post, true);
        
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Postdoc();
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.