Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.Content returns garbled content #2262

Open
colindcli opened this issue Sep 14, 2024 · 4 comments
Open

Response.Content returns garbled content #2262

colindcli opened this issue Sep 14, 2024 · 4 comments
Labels

Comments

@colindcli
Copy link

colindcli commented Sep 14, 2024

I use RestSharp version 112.0.0
net framework 4.6.2

Response.Content returns garbled content, but the result returned by the browser is not garbled. See the picture. The left side is the result returned by RestSharp, and the right side is the result returned by the browser source code. It is certain that the browser uses utf-8 decoding.

111

I analyzed the reason and I think the browser skips the utf-7 encoding, whether it is utf-7 returned in the content-type of the header or utf-7 set in the charset of the html code. I used the following code to verify it.

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-7">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-7">

    <meta charset="utf-8">
    <meta charset="gb2312">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-7">
    <meta charset="utf-7">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div>中文</div>
</body>
</html>

For this html code, the encoding obtained is utf-8. UTF-7 is not effective when it is both before and after.

@colindcli colindcli added the bug label Sep 14, 2024
@colindcli
Copy link
Author

colindcli commented Sep 14, 2024

There is nothing wrong with getting the ContentType encoding in the header in theory, but it is invalid for utf-7. Have you considered adding a parameter to RestClientOptions to define which encodings to ignore?

I hope that for ignored encoding, the default encoding utf-8 is used to output the content

@colindcli
Copy link
Author

Found the problem version: net framework 4.6.2
No problem version: net 6.0

@alexeyzimarev
Copy link
Member

RestSharp uses the value of the Encoding property of RestClientOptions if the encoding header is not provided in the response. It should be possible to add an interceptor to remove or change the response encoding header if it's not what you want.

Another alternative is to ignore the Content property and use the Stream property that contains the response content as-is, and you can then use the StreamReader with the encoding you want to get the actual content.

@colindcli
Copy link
Author

The ContentType of the request header returns utf-7 encoding, but the browser does not support utf-7 encoding, so the default utf-8 encoding is used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants