使用 httpwebrequest 调用 ibm watson 发现 adddocument api

联邦储备银行

我需要将 watson 发现功能集成到使用 Visual Studio 2015 框架 4.5 开发的现有 Windows 服务应用程序中。

我们无法升级框架版本,因此 nuget 库 ibm.watson 不起作用。

我尝试转换这个 curl 命令

curl -X POST -u "{username}":"{password}" -F "file=@c:\temp\1.json" https://gateway-fra.watsonplatform.net/discovery/api/v1/environments/{environment}/collections/{collection}/documents?version=2017-11-07

进入c#代码:

  void test
    {

        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        string username = "{username}";
        string password = "{password}";
        string postData = File.ReadAllText(@"c:\temp\1.json");

        string BASE_URL = "https://gateway-fra.watsonplatform.net/discovery/api/";
        string url = BASE_URL + "/v1/environments/{environment}/collections/{collection}/documents";

        var request = (HttpWebRequest)WebRequest.Create(url);

        var data = Encoding.ASCII.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/json";
        request.ContentLength = data.Length;
        request.Credentials = new NetworkCredential(username, password);

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new  StreamReader(response.GetResponseStream()).ReadToEnd();
 catch (WebException wex)
            {
                HttpWebResponse oHttpWebResponse = (HttpWebResponse)wex.Response;

                Encoding oEncoding = Encoding.GetEncoding(1252);
                string sEx = string.Empty;
                using (StreamReader oStreamReader = new StreamReader(oHttpWebResponse.GetResponseStream(), oEncoding))
                {
                    sEx = oStreamReader.ReadToEnd();
                    oStreamReader.Close();
                }
                System.Diagnostics.Debug.WriteLine(sEx);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
}

但得到这个错误:

{
  "code": 400,
  "error": "The request does not contain a \"file\" part or \"metadata\" part. Include at least one of those parts and resubmit your request."
}

添加文件或元数据以请求的方法是什么?

联邦储备银行

这对我有用:

async Task<string> Upload()
        {
            string environmentId = "xxx";
            string collectionId = "xxx";
            string username = "xxx";
            string password = "xxx";
            string sResult = string.Empty;
            string filename = @"c:\temp\watson\2824.json";
            string url = $"https://gateway-fra.watsonplatform.net/discovery/api/v1/environments/{environmentId}/collections/{collectionId}/documents?version=2018-08-01";
            FileStream fileStream = new FileStream(filename, FileMode.Open);
            HttpContent fileStreamContent = new StreamContent(fileStream);
            var credentials = new NetworkCredential(username, password);
            var handler = new HttpClientHandler { Credentials = credentials };
            using (var client = new HttpClient(handler))
            {
                using (var formData = new MultipartFormDataContent())
                {
                    formData.Add(fileStreamContent, "file", filename);
                    var response = await client.PostAsync(url, formData);
                    if (response.IsSuccessStatusCode)
                    {
                        Stream oStream = await response.Content.ReadAsStreamAsync();
                        using (var reader = new StreamReader(oStream))
                        {
                            sResult = reader.ReadToEnd();
                            reader.Close();
                        }
                    }
                }
            }
            fileStream.Close();
            return sResult;
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IBM Watson API Java SDK使用Watson令牌认证失败

如何通过REST API使用IBM Watson的QA服务

使用WebSockets的IBM Watson语音到文本

通过C#中的API使用IBM Watson Conversation

使用C#异步HttpWebRequest调用

Node.js-结合IBM Watson发现和对话服务

我如何使用IBM Watson Assistant显示视频

无法通过HTTP请求在Unity C#中调用外部API(IBM Watson)?

从IBM Watson API python遍历JSON结果时出错

IBM Watson Api视觉识别错误

在Postman中使用IBM Watson Text-to-Speech吗?

如何使用Plotly绘制IBM Watson NLU API JSON输出?

IBM Watson使用Postman翻译apikey授权失败

我们可以使用python(ibm_watson sdk)从“ ibm云函数”调用其他Watson服务(如助手,发现等)吗?

使用IBM Watson的Chatbot-如何验证日期输入?

Anaconda与IBM Watson Studio

IBM Watson NLU:如何通过API端点确定剩余的信用?

将IBM Watson API与jquery的$ .ajax一起使用

使用Python从IBM Watson Concept Insights调用annotate_text

如何对IBM Waston Speech to text API使用关键字发现功能?

IBM Watson Devices HTTP API连续提供403

使用IBM Watson Visual Recognition进行人脸识别

用于更新IBM Watson Conversation实体的API

使用 Java 将变量传递给 IBM Watson Conversation Dialog

您好,我如何使用 IBM Watson 对话显示使用 IBM Watson 链接到 facebook messenger 的图像?

如何使用 IBM watson 情感分析分析任何文本

使用 IBM Watson 的语音转文本会导致在识别关键字时多次调用方法

IBM-Watson 的“VisualRecognitionV3”使用问题

使用 Curl 遇到 IBM Watson TTS 问题