Read Http Request into Byte array

后端 未结 6 1569
情话喂你
情话喂你 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();
        }
    }
    

提交回复
热议问题