Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding an input stream

Tags:

So I have a page that is accepting XML through a POST method. Here's a small bit of the code:

if (Request.ContentType != "text/xml")
        throw new HttpException(500, "Unexpected Content Type");

StreamReader stream = new StreamReader(Request.InputStream);
string x = stream.ReadToEnd();  // added to view content of input stream

XDocument xmlInput = XDocument.Load(stream);

I was getting an error, so I converted the stream to a string, just to see if everything was being sent correctly. When I looked at the content, it looked like this:

%3c%3fxml+version%3d%271.0%27+encoding%3d%27UTF-8%27%3f%3e%0d%0a

So I guess I need to decode the stream. The only problem is that I don't know how I can use HtmlDecode on the stream, and still keep it as a StreamReader object.

Is there any way to do this?

like image 887
Steven Avatar asked Oct 31 '11 17:10

Steven


People also ask

How do you read input stream data?

InputStream. read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.

How do you convert an InputStream into String in java?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

How do you convert input streams to bytes?

Example 1: Java Program to Convert InputStream to Byte Arraybyte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.

What is the meaning of input stream?

1.1 InputStream: InputStream is an abstract class of Byte Stream that describe stream input and it is used for reading and it could be a file, image, audio, video, webpage, etc. it doesn't matter. Thus, InputStream read data from source one item at a time.


1 Answers

Apparently the client is sending the content as URL-encoded XML. So you need to decode the content like this:

StreamReader stream = new StreamReader(Request.InputStream);
string x = stream.ReadToEnd();
string xml = HttpUtility.UrlDecode(x);

XDocument xmlInput = XDocument.LoadXml(xml);

Anyway, the problem is probably on the client side... why is it encoding the XML this way?

like image 101
Thomas Levesque Avatar answered Sep 19 '22 15:09

Thomas Levesque