【问题标题】:How to connect to XML-RPC from c#如何从 C# 连接到 XML-RPC
【发布时间】:2012-02-16 00:48:08
【问题描述】:

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

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

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

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

怎么打电话?

【问题讨论】:

  • 你做了哪些研究?你试过什么?您是否遇到了我们可以帮助您解决的特定错误?
  • 可能重复:stackoverflow.com/questions/1348503/…。尽管他们至少表现出一点努力。
  • 这不是那个问题的重复,我想做的是向那个 api 发送 http 请求并请求输出。我是 XML-RPC 的新手,我不知道它是如何工作的。这是他们提供的文档pastebin.com/7qAX2t8m,我不知道如何输入变量并发送请求并得到响应,请您帮忙

标签: c# pandorabots


【解决方案1】:

这应该可以帮助您:

   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; }
}

【讨论】:

  • Result result = null; 中的“Result”是什么,它给我一个错误,它是数据类型吗?
  • YAH,我很糟糕,我看到了下面的公开声明,好的。谢谢它运行得很好,你能提供它的 C++ 版本吗,我试过了,但是函数给了我错误
  • 不,我不能。我不是 C++ 开发人员。你用 C# 标记了你的问题,这就是你得到的:-)。您可以将我的答案标记为正确,尝试让您自己实现 C++,当您遇到困难时提出一个新问题,显示您的代码并参考这个问题和答案。 Stackoverflow 不是 C++ 实现机器人 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多