![](/img/trans.png)
[英]What is the best way to correctly read a HTTP POST request from a socket byte stream?
[英]Read Http Request into Byte array
我正在開發一個需要接受 HTTP Post 請求並將其讀入字節數組以進行進一步處理的網頁。 我有點被困在如何做到這一點上,我很難完成什么是最好的方法。 到目前為止,這是我的代碼:
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[] 字段中。
謝謝!
最簡單的方法是將其復制到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);
}
}
你可以只使用 WebClient ......
WebClient c = new WebClient();
byte [] responseData = c.DownloadData(..)
其中..
是數據的 URL 地址。
我使用MemoryStream
和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();
我有一個函數可以通過在響應流中發送來完成它:
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());
對於 context.Request.ContentLength 大於零的所有情況,您可以簡單地執行以下操作:
byte[] contentBytes = context.Request.BinaryRead(context.Request.ContentLength);
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.