WebClient c# 发送 post with \n 而不换行
WebClient c# Send post with \n without making new line
我尝试发送一个包含 \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
如何让它输出: This is a Test\n This is a Test2 ?
尝试转义,传递 \n
而不是 \n
。那是你要的吗?尝试使用 Regex.Escape
\n 是一个 Escape Sequence
字符“\”是使用字符串时忽略的转义字符。它后面的字符通常会给它一个含义。 "\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#
string textToSend = "This is a Test"+Environment.NewLine+ "This is a Test2";
我尝试发送一个包含 \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
如何让它输出: This is a Test\n This is a Test2 ?
尝试转义,传递 \n
而不是 \n
。那是你要的吗?尝试使用 Regex.Escape
\n 是一个 Escape Sequence
字符“\”是使用字符串时忽略的转义字符。它后面的字符通常会给它一个含义。 "\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#
string textToSend = "This is a Test"+Environment.NewLine+ "This is a Test2";