When I’ve uploaded content from my C# app to a website in the past, I’ve used a POST request like so:
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();
My problem is that now I’m in a situation where this.message will very likely contain newlines, tabs, and special characters including ‘&’ and ‘=’. Do I need to escape this content. If so, how?
You can use HttpUtility.HtmlEncode/HtmlDecode or UrlEncode/UrlDecode as appropriate.