C# HttpWebRequest - 转义 POST 内容?

标签 c# httpwebrequest escaping

过去,当我将内容从我的 C# 应用程序上传到网站时,我使用了如下 POST 请求:

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://" + this.server + "/log.php");
    wr.Method = "POST";
    wr.ContentType = "application/x-www-form-urlencoded";
    string paramString = "v=" + this.version + "&m=" + this.message;
    wr.ContentLength = paramString.Length;
    StreamWriter stOut = new StreamWriter(wr.GetRequestStream(), System.Text.Encoding.ASCII);
    stOut.Write(paramString);
    stOut.Close();

我的问题是,现在我的情况是 this.message 很可能包含换行符、制表符和特殊字符,包括“&”和“=”。我需要转义这个内容吗?如果是,怎么办?

最佳答案

您可以根据需要使用 HttpUtility.HtmlEncode/HtmlDecode 或 UrlEncode/UrlDecode。

关于C# HttpWebRequest - 转义 POST 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/770355/

相关文章:

c# - Unity WWW web 请求未执行

bash - 在 sed 命令中使用变量的值

java - 如何自动取消转义字符串中的转义字符

c# - 通过 C# 和 UTF-8 字符集的 HttpWebRequest

c# - 如何使用友好名称而不是指纹获取 X509Certificate?

c# - .NET Core 2.1 Web Scraper控制台输出“HtmlAgilityPack.HtmlDocument

c# - AP.NET webforms 使用 LINQ to SQL 数据源和跨多个表的外键数据绑定(bind)

c# - 使用 HttpWebRequest 收到的截断响应

utf-8 - 如何在 Struts2 文本字段的标签中使用 HTML 转义字符?

c# - WPF ListBox 项目命令绑定(bind)