Search code examples
c#asp.nettextencodingdecoding

Decode Text from Request.InputStream in Asp.net C#


I'm trying to retrieve data from a hidden input of my HTML:

<INPUT type="hidden" name="HIDDENQUESMINV" id="HIDDENQUESMINV" value="@@Mínimo">

When I try to get its value by Request.InputStrem after submit the form I get this text: @@M%C3%ADnimo

My HTML is UTF-8 and it's good when I see it throught F12, but the request change the format. I tried to decode it, to change the encoding of the request but nothing works at all. I'm kinda lost of this and I wouldnt like to replace all this characters.

Thanks you, Miguel.


Solution

  • Your value is Percent encoded, as this is needed for transfer via HTTP protocol. You can decode it using

    string decoded_value = Uri.UnescapeDataString(value_from_input_stream);
    

    But I wonder why do you read value from InputStream? It is much easier to read value simply as

    string decoded_value = Request.Form["HIDDENQUESMINV"];
    

    if form is sent using POST method, or

    string decoded_value = Request.QueryString["HIDDENQUESMINV"];
    

    when form is sent using GET method. This way, values are already decoded, so you don't need to worry about it.