【问题标题】:How do I use the IBM Watson Dialog service using c#?如何使用 c# 使用 IBM Watson Dialog 服务?
【发布时间】:2015-10-29 12:28:52
【问题描述】:

一个客户正在使用 IBM 的 Watson Dialog 服务,我找不到任何人使用 .Net(特别是 c#)做最基本的事情的例子。

IBM 仅显示使用 Curl、Node 和 Java 的示例...

我的第一个目标是能够向 watson 服务添加一个新的 xml 文件(对话框树)。看起来很简单,但我已经有一段时间了。

【问题讨论】:

    标签: c# ibm-watson watson-dialog


    【解决方案1】:

    所以我最终通过在相关主题的十几个谷歌搜索的帮助下拼凑起来完成了这项工作。以为我会在这里发布工作版本。

    下面是我的代码,它将在 MVC 控制器中使用 C# 将 xml 文件上传到 Watson Dialog 服务。

    前端是一个表单,它采用友好名称(我将其转换为 .xml 文件名)和文件本身的上传(使用 dropzone)。

    我确信可以进行优化,但我希望这对某人有所帮助。好消息是,这可以用作执行任何 Watson Dialog 服务调用(添加、更新、删除)的基础。

        public ContentResult Save(FormCollection form)
        {
            try
            {
                var name = form["txtDialogName"];
    
                var filename = name + ".xml";
                byte[] bytes = null;
    
                foreach (string s in Request.Files)
                {
                    var file = Request.Files[s];
    
                    using (Stream inputStream = file.InputStream)
                    {
                        MemoryStream memoryStream = inputStream as MemoryStream;
                        if (memoryStream == null)
                        {
                            memoryStream = new MemoryStream();
                            inputStream.CopyTo(memoryStream);
                        }
    
                        bytes = memoryStream.ToArray();
                    }
    
                    break;
                }
    
                if (bytes == null)
                {
                    var contentResult = new ContentResult
                    {
                        ContentType = "application/json",
                        Content = null
                    };
                    return contentResult;
                }
    
    
                var basePath = ConfigurationManager.AppSettings["WatsonPath"];
    
                var username = ConfigurationManager.AppSettings["WatsonUsername"];
                var password = ConfigurationManager.AppSettings["WatsonPassword"];
                string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password)));
    
                var values = new[]
                             { new KeyValuePair<string, string>("name", filename) };
    
                using (var client = new HttpClient())
                using (var formData = new MultipartFormDataContent())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
    
                    foreach (var keyValuePair in values)
                    {
                        formData.Add(new StringContent(keyValuePair.Value), string.Format("\"{0}\"", keyValuePair.Key));
                    }
    
                    formData.Add(new ByteArrayContent(bytes),
                                   '"' + "file" + '"',
                                   '"' + filename + '"');
    
                    var response = client.PostAsync(basePath + "/v1/dialogs", formData).Result;
                    var result = response.Content.ReadAsStringAsync().Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        var contentResult = new ContentResult
                        {
                            ContentType = "application/json",
                            Content = response.ReasonPhrase
                        };
                        return contentResult;
                    }
    
                    var successResult = new ContentResult
                    {
                        ContentType = "application/json",
                        Content = result
                    };
    
                    return successResult;
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
                var contentResult = new ContentResult
                {
                    ContentType = "application/json",
                    Content = "false"
                };
                return contentResult;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多