Httpclient asp.net 核心 curl 等价物
Httpclient asp.net core curl equivalent
我对 ASP.Net HttpClient POST 请求有疑问。
事实上,我想使用 SolrCell 在 Solr 中索引文档。我像这样使用卷曲:
curl 'http://localhost:8983/solr/my_collection/update/extract?literal.id=doc1&commit=true' -F "myfile=@example/exampledocs/solr-word.pdf"
不幸的是,我只能将文件作为多部分文件上传发送(使用 HttpClient),这样我需要确定文件的 MIME 类型,我这样做了,但我仍然遇到 DOCX 和 PPTX 错误文件。
这是我的代码:
var fileBytes = File.ReadAllBytes(path);
requestContent.Add(new ByteArrayContent(fileBytes), "file");
requestContent.Headers.Remove("Content-Type");
requestContent.Headers.Add("Content-Type", contentType);
var response = await client.PostAsync(defaultSolrUri, requestContent);
return response.Content;
请帮忙。
我找到了解决办法!无需传递 MultiPartFormData,您需要做的就是在 PostAsyn 中将文件作为 ByteArrayContent 传递:
string path = "path/to/file";
var fileBytes = File.ReadAllBytes(path);
var response = await client.PostAsync(defaultSolrExtractUri, new ByteArrayContent(fileBytes));
return response.Content;
我对 ASP.Net HttpClient POST 请求有疑问。 事实上,我想使用 SolrCell 在 Solr 中索引文档。我像这样使用卷曲:
curl 'http://localhost:8983/solr/my_collection/update/extract?literal.id=doc1&commit=true' -F "myfile=@example/exampledocs/solr-word.pdf"
不幸的是,我只能将文件作为多部分文件上传发送(使用 HttpClient),这样我需要确定文件的 MIME 类型,我这样做了,但我仍然遇到 DOCX 和 PPTX 错误文件。
这是我的代码:
var fileBytes = File.ReadAllBytes(path);
requestContent.Add(new ByteArrayContent(fileBytes), "file");
requestContent.Headers.Remove("Content-Type");
requestContent.Headers.Add("Content-Type", contentType);
var response = await client.PostAsync(defaultSolrUri, requestContent);
return response.Content;
请帮忙。
我找到了解决办法!无需传递 MultiPartFormData,您需要做的就是在 PostAsyn 中将文件作为 ByteArrayContent 传递:
string path = "path/to/file";
var fileBytes = File.ReadAllBytes(path);
var response = await client.PostAsync(defaultSolrExtractUri, new ByteArrayContent(fileBytes));
return response.Content;