WebClient c#在不换行的情况下使用\n发送帖子

WebClient c#在不换行的情况下使用\n发送帖子,c#,webclient,C#,Webclient,我尝试发送一个包含一个\n的json,但当我发送它时,webclient会在其中做一个换行符\n,我想发送示例: using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.Accept] = "*/*"; wc.Headers[HttpRequestHeader.AcceptLanguage] =

我尝试发送一个包含一个\n的json,但当我发送它时,webclient会在其中做一个换行符\n,我想发送示例:

using (WebClient wc = new WebClient())
                {    
                    wc.Headers[HttpRequestHeader.Accept] = "*/*";
                    wc.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.5";
                    wc.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
                    wc.Headers[HttpRequestHeader.ContentType] = "application/json";

                    ServicePointManager.Expect100Continue = false;
                    string textToSend = "This is a Test\n This is a Test2"
                    string sendString = textToSend;
                    byte[] responsebytes = wc.UploadData("https://localhost/", "POST",
                    System.Text.Encoding.UTF8.GetBytes(sendString));

                    string sret = System.Text.Encoding.UTF8.GetString(responsebytes);
                }
输出:这是一个测试 这是一个测试2


如何将其输出:这是测试\n这是测试2?

尝试转义,通过
\\n
而不是
\n
。这就是你想要的吗?尝试使用

\n是一种

字符“\”是使用字符串时忽略的转义字符。它后面的字符通常会赋予它一个含义。“\n”只是指换行符

要逐字打印此序列,必须转义转义序列

使用转义字符,如下所示:

string textToSend = "This is a Test\\n This is a Test2";

不使用\n或\\n,使用插值字符串是安全的

 string textToSend = $"This is a Test{Environment.NewLine} This is a Test2";
或者如果您使用的是旧版本的C#


使用“\\n”转义“\n”,这样可以保留您的输入。谢谢!我一直在想这个,但出于某种原因,我从来没有测试过它。工作起来很有魅力!
string textToSend = "This is a Test"+Environment.NewLine+ "This is a Test2";