Read Http Request into Byte array

81,241

Solution 1

The simplest way is to copy it to a MemoryStream - then call ToArray if you need to.

If you're using .NET 4, that's really easy:

MemoryStream ms = new MemoryStream();
curContext.Request.InputStream.CopyTo(ms);
// If you need it...
byte[] data = ms.ToArray();

EDIT: If you're not using .NET 4, you can create your own implementation of CopyTo. Here's a version which acts as an extension method:

public static void CopyTo(this Stream source, Stream destination)
{
    // TODO: Argument validation
    byte[] buffer = new byte[16384]; // For example...
    int bytesRead;
    while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        destination.Write(buffer, 0, bytesRead);
    }
}

Solution 2

You can just use WebClient for that...

WebClient c = new WebClient();
byte [] responseData = c.DownloadData(..)

Where .. is the URL address for the data.

Solution 3

I use MemoryStream and Response.GetResponseStream().CopyTo(stream)

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
MemoryStream ms = new MemoryStream();
myResponse.GetResponseStream().CopyTo(ms);
byte[] data = ms.ToArray();

Solution 4

I have a function that does it, by sending in the response stream:

private byte[] ReadFully(Stream input)
{
    try
    {
        int bytesBuffer = 1024;
        byte[] buffer = new byte[bytesBuffer];
        using (MemoryStream ms = new MemoryStream())
        {
            int readBytes;
            while ((readBytes = input.Read(buffer, 0, buffer.Length)) > 0)
            {
               ms.Write(buffer, 0, readBytes);
            }
            return ms.ToArray();
        }
    }
    catch (Exception ex)
    {
        // Exception handling here:  Response.Write("Ex.: " + ex.Message);
    }
}

Since you have Stream str = curContext.Request.InputStream;, you could then just do:

byte[] bytes = ReadFully(str);

If you had done this:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(someUri);
req.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

You would call it this way:

byte[] bytes = ReadFully(resp.GetResponseStream());
Share:
81,241
Encryption
Author by

Encryption

Working as a professional consultant for the past 3 years, I do a lot of work in Java or .NET Frameworks. Experience in a variety of industries with a variety of tech stacks.

Updated on November 09, 2020

Comments

  • Encryption
    Encryption over 3 years

    I'm developing a web page that needs to take an HTTP Post Request and read it into a byte array for further processing. I'm kind of stuck on how to do this, and I'm stumped on what is the best way to accomplish. Here is my code so far:

     public override void ProcessRequest(HttpContext curContext)
        {
            if (curContext != null)
            {
                int totalBytes = curContext.Request.TotalBytes;
                string encoding = curContext.Request.ContentEncoding.ToString();
                int reqLength = curContext.Request.ContentLength;
                long inputLength = curContext.Request.InputStream.Length;
                Stream str = curContext.Request.InputStream;
    
             }
           }
    

    I'm checking the length of the request and its total bytes which equals 128. Now do I just need to use a Stream object to get it into byte[] format? Am I going in the right direction? Not sure how to proceed. Any advice would be great. I need to get the entire HTTP request into byte[] field.

    Thanks!

  • Encryption
    Encryption about 12 years
    How would I use memory stream without the .CopyTo option?
  • Jon Skeet
    Jon Skeet about 12 years
    @Encryption: You can implement something very similar yourself - I'll edit it in a minute...
  • Encryption
    Encryption about 12 years
    So to get it into byte[] would I then just need destination.ToArray?
  • Jon Skeet
    Jon Skeet about 12 years
    @Encryption: You can use the code in the first part of the answer at that point.
  • vapcguy
    vapcguy over 6 years
    Good code, but it assumes the data being fetched is string data. If it were a file or something else that needed to be kept as bytes, this wouldn't be the way to do it. I'll neither vote up nor down on this, though.
  • Andy
    Andy over 6 years
    Be aware that DownloadData will never throw exceptions for 4xx and 5xx
  • vapcguy
    vapcguy about 6 years
    Basically a duplicate of the accepted answer, just using a WebResponse, which the OP is not using.