Question

I'm trying to intercept .NET Remoting Request/Responses by implementing a ServerChannelSink. All is well, apart from the fact that I can't seem to decode the stream into a string. How do I do this?

Basically, in the watch window I can see that a value has been assigned to my variable after running the code: - enter image description here

But if I open the Text Visualizer it is empty. Similarly if I try to write the string to the Output window I don't get any lines written.

Here is the code that I'm using:

    private static void PrintStream(TextWriter output, ref Stream stream)
    {
        // If we can't reset the stream's position after printing its content,
        //   we must make a copy.
        if (!stream.CanSeek)
            stream = CopyStream(stream);

        long startPosition = stream.Position;

        byte[] buffer = new byte[stream.Length];

        stream.Read(buffer, 0, (int)stream.Length);

        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

        string request = enc.GetString(buffer, 0, buffer.Length);

        output.WriteLine(request);

        output.WriteLine();

        // set stream to previous position for message processing

        stream.Seek(startPosition, SeekOrigin.Begin);
    }

I've also tried using a StreamReader with the same result:

    private static void PrintStream(TextWriter output, ref Stream stream)
    {
        // If we can't reset the stream's position after printing its content,
        //   we must make a copy.
        if (!stream.CanSeek)
            stream = CopyStream(stream);

        long startPosition = stream.Position;

        StreamReader sr = new StreamReader(stream);
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            output.WriteLine(line);
        }

        stream.Position = startPosition;
    }
Was it helpful?

Solution 2

Thanks to paqogomez answer about \0's being interpreted as the end of the string, I just added the following: -

request = request.Replace("\0", "");

I now get this in the output window which is perfect for my purposes, thanks.

----------Request Headers-----------

__ConnectionId: 16

__IPAddress: 127.0.0.1

__RequestUri: /VisaOM.Server.ClientServices.Services

Content-Type: application/octet-stream

__CustomErrorsEnabled: False

----------Request Message-----------

get_SecurityServiceszVisaOM.Client.Services.IServices, VisaOM.Client.Services.Interfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

------End of Request Message--------

OTHER TIPS

application/octet-stream means binary. Your request variable contains binary data only some of which converts to human readable text so you cannot convert it to a string.

The best you could do is use Convert.ToBase64String to convert it to base 64 but it won't be human readable. Converting it to ASCII will corrupt the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top