首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从c#连接到XML-RPC

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

Stack Overflow用户
提问于 2012-01-22 11:00:09
回答 1查看 1.8K关注 0票数 0

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

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

http://www.pandorabots.com/pandora/talk-xml客户端需要发送的表单变量包括:

botid -参见上面的H.1。输入-你想对机器人说的话。custid -用于跟踪与特定客户的对话的ID。此变量是可选的。如果不发送值,Pandorabots将在返回的XML元素中返回custid属性值。在接下来的文章中使用这个来继续对话。

怎么打电话?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-22 21:38:04

这应该会让你继续前进:

代码语言:javascript
复制
   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; }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8958604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档