C# HTTP GET 请求返回隐藏字符

发布于 2024-10-03 19:28:19 字数 781 浏览 3 评论 0原文

我正在尝试从网络服务器获取一些数据。当我通过浏览器时,我看到如下响应:

[ { "x": "1" ,"y" : "2" ,"z" : "3" } ] 

当我发送 GET 请求时,结果返回:

[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

我使用的代码基本上是:

// Create a request for the URL.
WebRequest request = WebRequest.Create(fullUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();

有没有一种简单的方法可以去掉 " 之前的 \n 和 \ ,或者我必须对响应进行一些正则表达式/字符串操作吗

? 将要

I am trying to get some data from a web server. When I go via a browser I see a response like:

[ { "x": "1" ,"y" : "2" ,"z" : "3" } ] 

When I send a GET request the result comes back:

[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

The code I am using is basically:

// Create a request for the URL.
WebRequest request = WebRequest.Create(fullUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();

Is there an easy way to get rid of the \n and the \ before the " , or do I have to do some regex/string manipulation on the response?

Thanks,
Will

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

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

发布评论

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

评论(3

快乐很简单 2024-10-10 19:28:19
[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

可能就是您在 Visual Studio 调试器中看到的内容。你不需要摆脱任何东西。此外,您还可以稍微简化代码,因为示例中所有未处置的资源都可能泄漏非托管句柄:

using (var client = new WebClient())
{
    string responseFromServer = client.DownloadString(fullUrl);
}
[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

is probably what you see in Visual Studio debugger. You don't need to get rid of anything. Also you could simplify your code a bit because all those undisposed resources in your sample could leak unmanaged handles:

using (var client = new WebClient())
{
    string responseFromServer = client.DownloadString(fullUrl);
}
温折酒 2024-10-10 19:28:19

引号之前的 '\' 只是您将在调试器中看到的内容,代表转义引号 - 没有问题,因为它仅用于显示目的,并且在执行字符串操作时不会作为字符出现。

'\n' 字符是一个换行符,它实际上就在那里。如果您不希望它出现在字符串中,可以使用以下命令将其删除:

responseFromServer = responseFromServer.Replace("\n", string.Empty);

The '\' before the quotation marks is simply what you will see in the debugger and represents an escaped quotation mark - no problem as it is for display purposes only and will not be present as a character when performing string manipulations.

The '\n' character is a new-line character and this is actually there. If you do not want it in the string, you can remove it with the following:

responseFromServer = responseFromServer.Replace("\n", string.Empty);
二货你真萌 2024-10-10 19:28:19
[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

这是 IDE 转义的字符串表示形式,就像在代码中使用它时一样
\n 代表换行符,\" 如果转义引号,浏览器也会收到相同的字符串,但它将换行符显示为空格:)

[ {\n\"x\": \"1\"\n,\"y\" : \"2\"\n,\"z\" : \"3\"\n\n}\n]\n"

This is presentation of string by your IDE escaped as you would while using it in your code where
\n Represents Line Break and \" if escaped quotation mark, browser also receives same string but it displays Newlines as space :)

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