C# 如何通过API将文档上载到watson discovery services。ASP.net

C# 如何通过API将文档上载到watson discovery services。ASP.net,c#,asp.net-mvc,ibm-watson,watson-discovery,C#,Asp.net Mvc,Ibm Watson,Watson Discovery,我正在使用以下API将文件上载到watson discovery services。我得到的响应是不支持的文件格式,即使我提供了有效的类型。 API: 公共异步任务索引() { 使用(var httpClient=new httpClient()) { //添加基本身份验证 var authbytarray=Encoding.ASCII.GetBytes(“{auth key}”); var authString=Convert.ToBase64String(authByteArray); ht

我正在使用以下API将文件上载到watson discovery services。我得到的响应是不支持的文件格式,即使我提供了有效的类型。 API:

公共异步任务索引()
{
使用(var httpClient=new httpClient())
{
//添加基本身份验证
var authbytarray=Encoding.ASCII.GetBytes(“{auth key}”);
var authString=Convert.ToBase64String(authByteArray);
httpClient.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“基本”,authString);
var text=string.Empty;
var uri=”https://gateway.watsonplatform.net/discovery/api/v1/environments/{envid}/collections/{collectionid}/documents?version=2017-11-07”;
var content=新的MultipartFormDataContent();
var bytes=System.IO.File.ReadAllBytes(Server.MapPath(“~/Views/UploadDocument/civilwar-api1.html”);
var file=新的流内容(新的内存流(字节));
content.Headers.ContentType=MediaTypeHeaderValue.Parse(“text/html”);
添加(新的流内容(新的内存流(字节)),“文件”);
var response=wait-httpClient.PostAsync(uri,内容);
var text1=await response.Content.ReadAsStringAsync();
}
返回视图();
}
api响应为:
{
“代码”:415,
“错误”:“不支持的媒体类型”

}

试试
httpClient.DefaultRequestHeaders
接受

.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”)//ACCEPT header

正如您在Watson Developer Cloud中看到的,您可以使用

在这个存储库中,您可以看到如何使用Watson提供的每个服务

添加文档的方法:

#region Documents
        private void AddDocument()
        {
            Console.WriteLine(string.Format("\nCalling AddDocument()..."));
            using (FileStream fs = File.OpenRead(_filepathToIngest))
            {
                var result = _discovery.AddDocument(_createdEnvironmentId, _createdCollectionId, _createdConfigurationId, fs as Stream, _metadata);

                if (result != null)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
                    _createdDocumentId = result.DocumentId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }
        }
  • 了解更多关于

虽然只有代码才能解决问题,但一些解释对于理解解决方案以及将来如何解决类似问题有很大帮助。
#region Documents
        private void AddDocument()
        {
            Console.WriteLine(string.Format("\nCalling AddDocument()..."));
            using (FileStream fs = File.OpenRead(_filepathToIngest))
            {
                var result = _discovery.AddDocument(_createdEnvironmentId, _createdCollectionId, _createdConfigurationId, fs as Stream, _metadata);

                if (result != null)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
                    _createdDocumentId = result.DocumentId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }
        }