简体   繁体   中英

StreamReader replacing a non-breaking space with a + symbol

I'm taking fields from a webform and trying to save the inputs to the database. It is currently replacing a non-breaking space with a '+' symbol (ie "Test 2" = "Test+2") As you will see below I have a hack that replaces a + from the string and replaces it with a " ".

I know this is a short-sided solution. I would have thought the 'Encoding.GetEncoding("UTF-8") would have taken care of that. I'd like to find some sort of encoding that would account for any types of characters. See the code below:

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;
}

To solve the specific problem you are asking, use UrlDecode in your postedValues http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx

But you should really not parse the Request . Just use Request.Params , the values will be already parsed and decoded there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM