2012-03-30 61 views
24

我正在开发一个需要采用HTTP Post Request并将其读取到字节数组中进行进一步处理的Web页面。我一直在坚持如何做到这一点,而且我很难完成什么是最好的方法。这是我到目前为止的代码:将Http请求读入字节数组

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; 

     } 
     } 

我检查的要求和等于128现在做我只需要使用一个Stream对象进入它的byte []格式的总字节的长度?我正朝着正确的方向走吗?不知道如何继续。任何建议都会很棒。我需要将整个HTTP请求放到byte []字段中。

谢谢!

回答

50

最简单的方法是将其复制到MemoryStream - 如果需要,请致电ToArray

如果您使用.NET 4,这是非常简单:

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

编辑:如果你不使用.NET 4,你可以创建自己的CopyTo从实施的。下面是它作为一个扩展方法的版本:

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

我没有.CopyTo? – Encryption 2012-03-30 18:37:15

+0

如何在不使用.CopyTo选项的情况下使用内存流? – Encryption 2012-03-30 18:39:53

+0

@加密:你可以自己实现一些非常相似的东西 - 我将在一分钟内编辑它... – 2012-03-30 18:40:24

0
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

好的代码,但它假定正在提取的数据是字符串数据。如果它是一个需要保存为字节的文件或其他东西,这不是实现它的方法。尽管如此,我也不会投票赞成。 – vapcguy 2017-10-16 19:34:42

12

您可以只使用Web客户端为...

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

哪里..是URL地址中的数据。

+0

请注意,DownloadData不会为4xx和5xx引发异常 – Nathan 2017-11-09 06:34:22

0

我有这样做,通过响应流发送功能:

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); 
    } 
} 

既然你有Stream str = curContext.Request.InputStream;,然后你可以只是做:

byte[] bytes = ReadFully(str); 

如果你已经做到了这一点:

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

你会说它是这样:

byte[] bytes = ReadFully(resp.GetResponseStream());