最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Additional information: Proxy XmlRpcUrl attribute or Url property not set和error CS0592: Attribute ‘XmlRpcUrl’ is not valid on this declaration type

C# crifan 748浏览 0评论

【问题】

在折腾C#中实现metaWeblog的getPost方法,写了测试代码:

class openPost : XmlRpcClientProtocol
{
    [XmlRpcMethod("metaWeblog.getPost")]
    //http://xmlrpc.scripting.com/metaWeblogApi.html
    //metaWeblog.getPost (postid, username, password) returns struct
    //The blogid, username, password and publish params are as in the Blogger API. newPost returns a string representation of the post id, again as defined by the Blogger API. The struct is where the juice is.
    public string getPost(string postid, string username, string password)
    {
        return (string)this.Invoke("getPost", new object[] { postid, username, password });
    }
}

运行出错:

An unhandled exception of type ‘CookComputing.XmlRpc.XmlRpcMissingUrl’ occurred in CookComputing.XmlRpcV2.dll

Additional information: Proxy XmlRpcUrl attribute or Url property not set.

Additional information Proxy XmlRpcUrl attribute or Url property not set

【解决过程】

1.明显是缺少了XmlRpcUrl的设置。

所以就去添加了对应的设置:

[XmlRpcUrl(https://www.crifan.com/xmlrpc.php)]

但是编译的时候又出错:

D:\xxx\OpenPostViaPermaLink\openPost.cs(73,10): error CS0592: Attribute ‘XmlRpcUrl’ is not valid on this declaration type. It is only valid on ‘class, interface’ declarations.

2.网上找了半天,看到这里:

MetaWeblogAPI C# Code Sample

然后就修改了对应代码,经过一番折腾,用如下代码:

using System;
using System.Collections.Generic;
using System.Text;
using CookComputing.XmlRpc;

namespace OpenPostViaPermaLink
{
    [XmlRpcUrl("https://www.crifan.com/xmlrpc.php")]
    class CrifanMetaWeblog : XmlRpcClientProtocol
    {
        [XmlRpcMethod("metaWeblog.getPost")]
        //http://xmlrpc.scripting.com/metaWeblogApi.html
        //metaWeblog.newPost (blogid, username, password, struct, publish) returns string
        //metaWeblog.editPost (postid, username, password, struct, publish) returns true
        //metaWeblog.getPost (postid, username, password) returns struct
        //The blogid, username, password and publish params are as in the Blogger API. newPost returns a string representation of the post id, again as defined by the Blogger API. The struct is where the juice is.
        public Object getPost(string postid, string username, string password)
        {
            Object retInfo = null;
            retInfo = this.Invoke("getPost", new object[] { postid, username, password });
            return retInfo;
        }
    }   
}

// calling function
void xmlRpcTest()
{
    CrifanMetaWeblog crifanBlog = new CrifanMetaWeblog();

    string postid = "7064";
    string username = "yourname";
    string password = "yourpassword";
    Object retPostInfo = crifanBlog.getPost(postid, username, password);
}

最后可以成功获得对应帖子的信息了

retInfo    Count = 22    object {CookComputing.XmlRpc.XmlRpcStruct}

[“permaLink”]    “https://www.crifan.com/tmp_test_open_post_via_permanent_link/”
[“wp_post_format”]    “standard”
[“postid”]    7064
[“wp_password”]    “”
[“wp_author_display_name”]    “crifan”
[“date_created_gmt”]    {4/2/2012 2:25:00 AM}
[“mt_text_more”]    “”
[“mt_excerpt”]    “”
[“description”]    “<p>内容:用于测试通过帖子固定链接打开帖子。</p>”
[“mt_allow_pings”]    1
[“title”]    “title of test open post via permenant link”
[“wp_slug”]    “tmp_test_open_post_via_permanent_link”
[“wp_author_id”]    “1”
[“userid”]    “1”
[“mt_keywords”]    “tmp”
[“post_status”]    “publish”
[“link”]    “https://www.crifan.com/tmp_test_open_post_via_permanent_link/”
[“dateCreated”]    {4/2/2012 10:25:00 AM}
[“custom_fields”]    {object[1]}
[“sticky”]    false
[“categories”]    {string[1]}
[“mt_allow_comments”]    1

剩下的,就可以一点点实现其他所需要的功能了,包括editPost,newPost等。

转载请注明:在路上 » 【已解决】Additional information: Proxy XmlRpcUrl attribute or Url property not set和error CS0592: Attribute ‘XmlRpcUrl’ is not valid on this declaration type

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.098 seconds, using 18.82MB memory