C# 将Http请求读入字节数组
我正在开发一个网页,它需要接受一个HTTPPOST请求,并将其读入字节数组以进行进一步处理。我有点被困在如何做到这一点上,我被什么是最好的方式来完成这一点难倒了。以下是我目前的代码:C# 将Http请求读入字节数组,c#,asp.net,httpcontext,system.web,C#,Asp.net,Httpcontext,System.web,我正在开发一个网页,它需要接受一个HTTPPOST请求,并将其读入字节数组以进行进一步处理。我有点被困在如何做到这一点上,我被什么是最好的方式来完成这一点难倒了。以下是我目前的代码: public override void ProcessRequest(HttpContext curContext) { if (curContext != null) { int totalBytes = curContext.Request.T
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)。现在我是否只需要使用流对象将其转换为byte[]格式?我走的方向对吗?我不知道怎么继续。任何建议都很好。我需要将整个HTTP请求放入byte[]字段
谢谢 最简单的方法是将其复制到
内存流中
——然后在需要时调用到阵列
如果您使用的是.NET 4,这非常简单:
MemoryStream ms = new MemoryStream();
curContext.Request.InputStream.CopyTo(ms);
// If you need it...
byte[] data = ms.ToArray();
编辑:如果您不使用.NET4,您可以创建自己的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);
}
}
您可以使用WebClient来实现这一点
WebClient c = new WebClient();
byte [] responseData = c.DownloadData(..)
其中
。
是数据的URL地址。我有一个函数,通过在响应流中发送:
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());
我使用MemoryStream
和Response.GetResponseStream().CopyTo(流)
对于context.Request.ContentLength大于零的所有这些情况,您只需执行以下操作:
byte[] contentBytes = context.Request.BinaryRead(context.Request.ContentLength);
在没有.CopyTo选项的情况下,我如何使用内存流?@Encryption:您可以自己实现一些非常类似的东西-我会在一分钟内编辑它…因此,要将其放入字节[],我只需要destination.ToArray吗?@Encryption:您可以在此时使用答案第一部分中的代码。代码很好,但它假定正在获取的数据是字符串数据。如果它是一个文件或其他需要保留为字节的东西,那么这就不是实现它的方法。不过,我不会对此投赞成票,也不会投反对票。请注意,DownloadData
不会对4xx和5xx抛出异常,基本上是接受答案的副本,只需使用OP未使用的WebResponse
。
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();
byte[] contentBytes = context.Request.BinaryRead(context.Request.ContentLength);