【asp.net core】Asp.net中C#调用xml-rpc远程发布文章

更新时间:2019-05-18    来源:word    手机版     字体:

【www.bbyears.com--word】

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
{
    ///


    /// This struct represents the information about a post that could be returned by the
    /// EditPost(), GetRecentPosts() and GetPost() methods.
    ///

    [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();
        }
    }

本文来源:http://www.bbyears.com/bangongshuma/51738.html

热门标签

更多>>

本类排行