繁体   English   中英

如何从C#连接到XML-RPC

[英]How to connect to XML-RPC from c#

如何从c#连接到XML-RPC Api

客户端可以通过POST'与Pandorabot交互:

http://www.pandorabots.com/pandora/talk-xml客户端需要POST的表单变量是:

botid-参见上文H.1。 输入-您要对机器人说的内容。 监督-跟踪与特定客户的对话的ID。 此变量是可选的。 如果您不发送值,Pandorabots将在返回的XML元素中返回一个custid属性值。 在后续的POST中使用它继续对话。

怎么打?

这应该可以帮助您:

   public void Talk()
   {
        string xmlResult = null;
        Result result = null;  // Result declared at the end 
        string botId = "c49b63239e34d1"; // enter your botid
        string talk = "Am I a human?"; 
        string custId = null; // (or a value )
        using (var wc = new WebClient())
        {
            var col = new NameValueCollection();

            col.Add("botid", botId);
            col.Add("input", talk);
            if (!String.IsNullOrEmpty(custId))
            {
                col.Add("custid", custId);
            }

            byte[] xmlResultBytes = wc.UploadValues(
                @"http://www.pandorabots.com/pandora/talk-xml", 
                "POST", 
                col);
            xmlResult = UTF8Encoding.UTF8.GetString(xmlResultBytes);
            result = Result.GetInstance(xmlResultBytes);
        }

        //raw result
        Console.WriteLine(xmlResult);

        // use the Result class
        if (result.status == 0)  // no error
        {
            Console.WriteLine("{0} -> {1}", 
                result.input, result.that);
        }
        else  // error
        {
            Console.WriteLine("Error: {0} : {1}", 
                result.input, result.message);
        }
    }


[XmlRoot(ElementName="result")]
public class Result
{
    static XmlSerializer ser = new XmlSerializer(typeof(Result) , "");

    public Result()
    {
    }

    public static Result GetInstance(byte[] bytes)
    {
        return (Result)ser.Deserialize(new MemoryStream(bytes));
    }

    [XmlAttribute]
    public int status { get; set; }
    [XmlAttribute]
    public string botid { get; set; }
    [XmlAttribute]
    public string custid { get; set; }
    [XmlElement]
    public string input { get; set; }
    [XmlElement]
    public string that { get; set; }
    [XmlElement]
    public string message { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM