HTTPWebResponse - String back to Raw Data?

Spilled

Member
Joined
Nov 28, 2008
Messages
17
Location
USA
Programming Experience
Beginner
Hi, My application makes a POST to a website that responds with encrypted data. When I use httpwebrequest/httpwebresponse and Encoding.UTF8.GetBytes() to convert the string back to a byte array and pass it to my decrypt function along with the key the decryption fails. Now, I know my decryption function is working properly because if i use tcpclient and make the POST the hard way and parse the data manually, pass it to my decryption method it works so my question to you is how can I get the response from httpwebresponse back as a raw byte array or how can i convert the string back? Here is how I am handling the response:

VB.NET:
                Response = (HttpWebResponse)Request.GetResponse();
                Stream receiveStream = Response.GetResponseStream();
                StreamReader readStream = new StreamReader(receiveStream,
                System.Text.Encoding.UTF8);
                string sResponse = readStream.ReadToEnd();
 
Thats my problem, I'm trying to get the original binary data and still use httpwebresponse/httpwebrequest. Is there a way to do this?
 
Moderation note, this is a VB.Net forum, use VB.Net language when posting code here.
 
Thats my problem, I'm trying to get the original binary data and still use httpwebresponse/httpwebrequest. Is there a way to do this?

Of course there is. In your code, 'receivedStream' is a Stream and all you can get from a Stream is binary data. Your StreamReader is actually reading that binary data and converting it to text. If you don't want text then don't use a StreamReader. Just read the binary data directly from the Stream. There is no method that will just read all the data available from a Stream so, like the StreamReader does internally, you need to read it in blocks. Here's a method that will do just that:
Private Function ReadToEnd(source As Stream) As Byte()
    'Read the data in 4K blocks.
    Const BUFFER_SIZE As Integer = 4096

    Dim buffer(BUFFER_SIZE - 1) As Byte
    Dim tempData As New List(Of Byte)

    'Read the first block of data.
    Dim bytesReadCount = source.Read(buffer, 0, BUFFER_SIZE)

    'Keep reading as long as the buffer was filled, indicating that there may be more data.
    Do While bytesReadCount = BUFFER_SIZE
        'Append the last block to the full list.
        tempData.AddRange(buffer)

        'Read the next block of data.
        bytesReadCount = source.Read(buffer, 0, BUFFER_SIZE)
    Loop

    'Check whether there was any data retrieved on the last read.
    If bytesReadCount > 0 Then
        Dim partialBuffer(bytesReadCount - 1) As Byte

        'Get just the data that was read from the buffer.
        Array.Copy(buffer, partialBuffer, bytesReadCount)

        'Append the last block.
        tempData.AddRange(partialBuffer)
    End If

    Return tempData.ToArray()
End Function
 
Moderation note, this is a VB.Net forum, use VB.Net language when posting code here.
I didn't even notice that myself. That's what can happen when you're using multiple forums in multiple languages at the same time: you forget where you are.
Ok, Ill port it next time sorry.
You don't have to port it. Notice at the top of the page is a link to our sister site for C# questions. Just post there instead.
 
Back
Top