0
我正在從Webform中取出字段並嘗試將輸入保存到數據庫。它正在用一個'+'符號代替一個非破壞性空間(即「測試2」=「測試+ 2」)正如你將在下面看到的,我有一個黑客攻擊從字符串中取代了+,並用「 」。StreamReader用+符號替換非破壞空間
我知道這是一個短暫的解決方案。我原以爲'Encoding.GetEncoding(「UTF-8」)會照顧到這一點。我想找到某種編碼來解釋任何類型的字符。請參見下面的代碼:
private List<Alias> GetAliasesFromPost()
{
List<Alias> aliases = new List<Alias>();
using (StreamReader sr = new StreamReader(Request.InputStream, Encoding.GetEncoding("UTF-8")))
{
string[] postedValues = sr.ReadLine().Split('&');
for (int i = 0; i < postedValues.Length; i++)
{
if (postedValues[i].StartsWith("urlAlias="))
{
Alias alias = new Alias();
string active = postedValues[i - 1].Replace("active=", string.Empty);
alias.Active = string.Compare(active, "on", true) == 0;
alias.UrlAlias = postedValues[i].Replace("urlAlias=", string.Empty);
alias.Notes = postedValues[i + 1].Replace("notes=", string.Empty).Replace("+", " ");
int Id = 0;
Int32.TryParse(postedValues[i + 2].Replace("id=", string.Empty), out Id);
alias.Id = Id;
aliases.Add(alias);
}
}
}
return aliases;
}
對不起,我是StreamReaders的新手,我添加了新的使用(StreamReader sr = new StreamReader(Request.Params);它似乎不喜歡過載。 –