httpClient ReadAsStringAsync 编码问题

huangapple go评论71阅读模式
英文:

httpClient ReadAsStringAsync encoding problem

问题

我已经创建了一个返回DOCX、DOC、RTF、PDF文件的Net Core 3.1 API,这些文件是从一个字符串生成的。我的问题是编码。从命令行使用CURL没有问题,但是使用我的API时,尽管在记事本中打开的文件代码看起来相似,但存在以下差异:

  1. 通过CURL生成的有效文件在记事本中以ANSI编码打开,而我的文件以UTF-8编码打开。

  2. 普通字母在两种情况下都相同,只有带有短划线和点的字母不同。

我尝试了不同的编码更改,但没有帮助,请帮忙。
要检索文件的内容,我使用以下代码:

string response = response.Content.ReadAsStringAsync().Result;

我已经尝试了一些编码更改,但目前还没有成功。

英文:

I've created a Net Core 3.1 API that returns DOCX, DOC, RTF, PDF files, generated from a string. My problem is encoding. Using CURL from the command line is no problem, but using my api, although the code of files opened in notepad is seemingly similar, there are differences:

  1. Valid file with CURL Notepad opens with ANSI encoding and mine with UTF-8

  2. Normal letters are the same in both cases, only those with dashes and dots differ
    enter image description here

I tried different coding changes but nothing helped, please help.
To retrieve the content of the file, I use:

string response = response.Content.ReadAsStringAsync().Result;

I've tried a few encoding changes but so far it hasn't worked.

答案1

得分: 1

我的问题是编码。

我认为问题不止于此。我认为问题在于你试图将不透明的二进制数据视为字符串来处理。

虽然RTF文件确实是文本,但你提到的所有其他格式(DOCX、DOC、PDF)都是二进制格式。尝试将任意二进制数据视为文本几乎总是会丢失信息。你不应该试图将数据存储为字符串 - 你应该将其读取为流或字节数组。目前还不清楚你在之后要做什么,但如果你要将其保存到文件中,我建议打开一个文件流,将响应作为流打开,然后只需使用 Stream.CopyTo 从响应复制到文件中。

(顺便提一下,你几乎肯定应该使用 await 而不是 Task.Result 属性。)

英文:

> My problem is encoding.

I think it's more than that. I think it's that you're trying to treat opaque binary data as a string to start with.

While RTF files are indeed text, all the other formats you mentioned (DOCX, DOC, PDF) are binary. Trying to treat arbitrary binary data as text will almost always lose information. You shouldn't be trying to store the data as a string - you should be reading it as a stream or a byte array. It's not clear what you're doing with the result afterwards, but if you're saving it to a file, I'd suggest opening a file stream, opening the response as a stream, and just using Stream.CopyTo to copy from the response to a file.

(As an aside, you should almost certainly be using await rather than the Task.Result property too.)

答案2

得分: 0

以下是翻译好的部分:

你可以使用类似这样的代码:

var client = new HttpClient();
var response = await client.GetByteArrayAsync(uri);
var responseString = Encoding.Unicode.GetString(response, 0, response.Length - 1);
return responseString;
英文:

You may use something like this:

var client = new HttpClient();
var response = await client.GetByteArrayAsync(uri);
var responseString = Encoding.Unicode.GetString(response, 0, response.Length - 1);
return responseString;

huangapple
  • 本文由 发表于 2023年7月10日 15:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定