c# 创建 XML-RPC客户端和博客通信

c# 创建 XML-RPC客户端和博客通信  可以创建博客文章删除等 Wordpress就是最好的李子

1、下载XML-PRC for .net 包,工程中引用CookComputing.XmlRpcV2.dll
2、接口文件

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Windows.Forms;  
  4.   
  5. using SMSServer;  
  6. using CookComputing.XmlRpc;  
  7.   
  8. namespace SMSServer.Service  
  9. {  
  10.     #region 测试结构体  
  11.   
  12.     //忽略某字段  
  13.     //public struct SampleStruct  
  14.     //{  
  15.     //    public string title;  
  16.     //    public string link;  
  17.     //    [XmlRpcMissingMapping(MappingAction.Ignore)]  
  18.     //    public string description;  
  19.     //}   
  20.   
  21.   
  22.     //某字段必填  
  23.     //[XmlRpcMissingMapping(MappingAction.Ignore)]  
  24.     //public struct SampleStruct  
  25.     //{  
  26.     //    [XmlRpcMissingMapping(MappingAction.Error)]  
  27.     //    public string title;  
  28.     //    [XmlRpcMissingMapping(MappingAction.Error)]  
  29.     //    public string link;  
  30.     //    public string description;  
  31.     //}   
  32.   
  33.     public struct sTest  
  34.     {  
  35.         public int a;  
  36.         public string b;  
  37.         public DateTime c;  
  38.         [XmlRpcMissingMapping(MappingAction.Ignore)]  
  39.         //public char d;  
  40.         public byte[] e;  
  41.         public bool f;  
  42.     }  
  43.     #endregion  
  44.   
  45.   
  46.     public interface ISMSService  
  47.     {  
  48.         [XmlRpcMethod("SMSServer.Lee")]  
  49.         string Lee(string str);  
  50.     }  
  51.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  52.     public class SMSService : MarshalByRefObject, ISMSService  
  53.     {  
  54.         public string Lee(string str)  
  55.         {  
  56.             return DateTime.Now.ToString() + ":" + str;  
  57.         }  
  58.     }  
  59. }  
  60. 3、创建启动和关闭RPC服务
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Runtime.Remoting;  
  10. using System.Runtime.Remoting.Channels;  
  11. using System.Runtime.Remoting.Channels.Http;  
  12. using System.Threading;  
  13. using System.IO;  
  14. using System.Runtime.InteropServices;  
  15.   
  16. using CookComputing.XmlRpc;  
  17. using SMSServer.Service;  
  18.   
  19. namespace SMSServer  
  20. {  
  21.     public partial class FrmMain : Form  
  22.     {  
  23.   
  24.         public HttpChannel channel;  
  25.   
  26.           
  27.   
  28.         public FrmMain()  
  29.         {  
  30.             InitializeComponent();  
  31.         }  
  32.   
  33.           
  34.   
  35.         private void FrmMain_Load(object sender, EventArgs e)  
  36.         {  
  37.             //button1.Visible = false;  
  38.             IDictionary props = new Hashtable();  
  39.             props["name"] = "SMSHttpChannel";  
  40.             props["port"] = 5678;  
  41.             channel = new HttpChannel(  
  42.                props,  
  43.                null,  
  44.                new XmlRpcServerFormatterSinkProvider()  
  45.             );  
  46.             ChannelServices.RegisterChannel(channel, false);  
  47.             RemotingConfiguration.RegisterWellKnownServiceType(  
  48.                  typeof(SMSService),  
  49.                  "sms.rem",  
  50.                  WellKnownObjectMode.Singleton);  
  51.         }  
  52.   
  53.         private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)  
  54.         {  
  55.             ChannelServices.UnregisterChannel(channel);  
  56.         }  
  57.   
  58.     }  
  59. }  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

你可能感兴趣的:(c# 创建 XML-RPC客户端和博客通信)