我正在尝试与不支持multipart/form-data上传文件的API交互。 我已经能够通过旧版WebClient使其工作,但由于它已被弃用,我想利用更新的HttpClient。 使用WebClient与此端点配合使用的代码如下:
using (WebClient client = new WebClient())
{
byte[] file = File.ReadAllBytes(filePath);
client.Headers.Add("Authorization", apiKey);
client.Headers.Add("Content-Type", "application/pdf");
byte[] rawResponse = client.UploadData(uploadURI.ToString(), file);
string response = System.Text.Encoding.ASCII.GetString(rawResponse);
JsonDocument doc = JsonDocument.Parse(response);
return doc.RootElement.GetProperty("documentId").ToString();
}
我还没有找到一种使用 HttpClient 实现等效上传的方法,因为它似乎总是使用 multipart.