使用c#httpclient在没有多部分/form-data的情况下发布文件

发布于 2025-01-28 14:45:15 字数 807 浏览 2 评论 0原文

我正在尝试与不支持多部分/form-data上传文件的API进行交互。

我已经能够将其与较旧的网络限制配合使用,但是由于它被贬低了,所以我想利用较新的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。

I'm trying to interact with a API that doesn't support multipart/form-data for uploading a file.

I've been able to get this to work with the older WebClient but since it's being deprecated I wanted to utilize the newer HttpClient.

The code I have for WebClient that works with this end point looks like this:

            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();
            }

I've not found a way to get an equivalent upload to work with HttpClient since it seems to always use multipart.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2025-02-04 14:45:15

我认为看起来像这样

using var client = new HttpClient();

var file = File.ReadAllBytes(filePath);

var content = new ByteArrayContent(file);
content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

var result = await client.PostAsync(uploadURI.ToString(), content);
result.EnsureSuccessStatusCode();

var response = await result.Content.ReadAsStringAsync();
var doc = JsonDocument.Parse(response);

return doc.RootElement.GetProperty("documentId").ToString();

I think it would look something like this

using var client = new HttpClient();

var file = File.ReadAllBytes(filePath);

var content = new ByteArrayContent(file);
content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

var result = await client.PostAsync(uploadURI.ToString(), content);
result.EnsureSuccessStatusCode();

var response = await result.Content.ReadAsStringAsync();
var doc = JsonDocument.Parse(response);

return doc.RootElement.GetProperty("documentId").ToString();
孤星 2025-02-04 14:45:15

什么说反对使用简单地使用HTTPClient的 postasync 方法与 bytearraycontent 结合使用?

byte[] fileData = ...;

var payload = new ByteArrayContent(fileData);
payload.Headers.Add("Content-Type", "application/pdf");

myHttpClient.PostAsync(uploadURI, payload);

What speaks against using simply HttpClient's PostAsync method in conjunction with ByteArrayContent?

byte[] fileData = ...;

var payload = new ByteArrayContent(fileData);
payload.Headers.Add("Content-Type", "application/pdf");

myHttpClient.PostAsync(uploadURI, payload);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文