0

c# から XML-RPC Api に接続する方法 ,

クライアントは、POST することで Pandorabot とやり取りできます。

http://www.pandorabots.com/pandora/talk-xml クライアントが POST するために必要なフォーム変数は次のとおりです。

botid - 上記の H.1 を参照してください。入力 - ボットに伝えたいこと。custid - 特定の顧客との会話を追跡するための ID。この変数はオプションです。値を送信しない場合、Pandorabots は返された XML の要素で custid 属性値を返します。後続の POST でこれを使用して、会話を続けます。

呼び方は?

4

1 に答える 1

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; }
}
于 2012-01-22T13:38:04.403 に答える