0

在发布数据时,我遇到了问题。问题在于 & 字符。我正在发布一个可能包含任何内容的字符串。像“ this&this, how are you?”。但在上述情况下,只有“ this”正在发送。字符中的字符串&被剥离。

我试过的代码:

  var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://mywebsite.com/import.php");
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write("tname=sanam&temail=sanam@" + Guid.NewGuid().ToString("N") + ".com&tbody=" + this.body + "&ttitle=" + this.title);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    retStr = streamReader.ReadToEnd();
                }
                //retStr = "POST: " + this.body;

            }

有人知道如何使用 C# 向服务器发送任何内容吗?提前致谢。

4

1 回答 1

3

对自由文本字段进行编码

System.Web.HttpUtility.UrlEncode()

IE:

streamWriter.Write("tname=sanam&temail=sanam@" + Guid.NewGuid().ToString("N") + ".com&tbody=" + HttpUtility.UrlEncode(this.body) + "&ttitle=" + HttpUtility.UrlEncode(this.title));
于 2013-06-26T14:46:53.500 回答