C# HTTP GET 请求返回隐藏字符
我正在尝试从网络服务器获取一些数据。当我通过浏览器时,我看到如下响应:
[ { "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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能就是您在 Visual Studio 调试器中看到的内容。你不需要摆脱任何东西。此外,您还可以稍微简化代码,因为示例中所有未处置的资源都可能泄漏非托管句柄:
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:
引号之前的 '\' 只是您将在调试器中看到的内容,代表转义引号 - 没有问题,因为它仅用于显示目的,并且在执行字符串操作时不会作为字符出现。
'\n' 字符是一个换行符,它实际上就在那里。如果您不希望它出现在字符串中,可以使用以下命令将其删除:
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:
这是 IDE 转义的字符串表示形式,就像在代码中使用它时一样
\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 :)