Read Http Request into Byte array

后端 未结 6 1545
情话喂你
情话喂你 2021-02-05 01:15

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

相关标签:
6条回答
  • 2021-02-05 01:40
    class WebFetch
    {
    static void Main(string[] args)
    {
        // used to build entire input
        StringBuilder sb = new StringBuilder();
    
        // used on each read operation
        byte[] buf = new byte[8192];
    
        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(@"http://www.google.com/search?q=google");
    
        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();
    
        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();
    
        string tempString = null;
        int count = 0;
    
        do
        {
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);
    
            // make sure we read some data
            if (count != 0)
            {
                // translate from bytes to ASCII text
                tempString = Encoding.ASCII.GetString(buf, 0, count);
    
                // continue building the string
                sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?
    
        // print out page source
        Console.WriteLine(sb.ToString());
        Console.Read();
        }
    }
    
    0 讨论(0)
  • 2021-02-05 01:41

    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());
    
    0 讨论(0)
  • 2021-02-05 01:47

    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();
    
    0 讨论(0)
  • 2021-02-05 01:54

    You can just use WebClient for that...

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

    Where .. is the URL address for the data.

    0 讨论(0)
  • 2021-02-05 01:55

    For all those cases when your context.Request.ContentLength is greather than zero, you can simply do:

    byte[] contentBytes = context.Request.BinaryRead(context.Request.ContentLength);
    
    0 讨论(0)
  • 2021-02-05 01:58

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题