0

I have MemoryStream data (HTML POST Data) which i need to parse it. Converting it to string give result like below

key1=value+1&key2=val++2

Now the problem is that all this + are space in html. Am not sure why space is converting to +

This is how i am converting MemoryStream to string

Encoding.UTF8.GetString(request.PostData.ToArray())
user1807378
  • 13
  • 1
  • 4

2 Answers2

0

If you are using Content-Type of application/x-www-form-urlencoded, your data needs to be url encoded.

Use System.Web.HttpUtility.UrlEncode():

using System.Web;
var data = HttpUtility.UrlEncode(request.PostData);

See more in MSDN.

You can also use JSON format for POST.

sheey
  • 162
  • 8
  • Hello, i am using **MiniHttpd** lib. request.PostData is MemoryStream and not string. And Content-Type i am getting is **text/plain;charset=UTF-8** – user1807378 May 22 '20 at 09:51
0

I suppose that the data you are retrieving are encoded with URL rules.
You can discover why data are encoded to this format reading this simple article from W3c school.

To encode/decode your post string you may use this couple of methods:

System.Web.HttpUtility.UrlEncode(yourString); // Encode
System.Web.HttpUtility.UrlDecode(yourString); // Decode

You can find more informations about URL manipulation functions here.

Note: If you need to encode/decode an array of string you need to enumerate your collection with a for or foreach statement. Remember that with this kind of cycles you cannot directly change the cycle variable value during the enumeration (so probably you need a temporary storage variable).

At least, to efficiently parse strings, I suggest you to use the System.Text.RegularExpression.Regex class and learn the regex "language".
You can find some example on how to use Regex here; Regex101 site has also a C# code generator that shows you how to translate your regex into code.

LoxLox
  • 977
  • 9
  • 16
  • Hello, i am using MiniHttpd lib. request.PostData is MemoryStream and not string. And Content-Type i am getting is text/plain;charset=UTF-8 – user1807378 May 22 '20 at 10:43
  • You can convert the memorystream object to a common string. Watch this: https://stackoverflow.com/questions/78181/how-do-you-get-a-string-from-a-memorystream – LoxLox May 24 '20 at 23:27
  • Or you can simply read the stream: string postDataString = null; using (StreamReader reader = new StreamReader(request.PostData)) postDataString = reader.ReadToEnd(); – LoxLox May 24 '20 at 23:31