簡體   English   中英

Api MemoryStream c# ,這段代碼如何工作?

[英]Api MemoryStream c# , how this code works?

我在這里提出一個問題,我知道這對您來說很簡單,但實際上我需要逐行了解它的作用。

using (var stream = new MemoryStream())
{
    var context = (System.Web.HttpContextBase)Request.Properties["MS_HttpContext"];
    context.Request.InputStream.Seek(0, SeekOrigin.Begin);
    context.Request.InputStream.CopyTo(stream);
    strContent = Encoding.UTF8.GetString(stream.ToArray());
    AppLog.Write("Request content length: " + strContent.Length);
}
// create a MemoryStream to act as a buffer for some unspecified data
using (var stream = new MemoryStream())
{
    // obtain the http-context (not sure this is a good way to do it)
    var context = (System.Web.HttpContextBase)Request.Properties["MS_HttpContext"];
    // reset the context's input stream to the start
    context.Request.InputStream.Seek(0, SeekOrigin.Begin);
    // copy the input stream to the buffer we allocated before
    context.Request.InputStream.CopyTo(stream);
    // create an array from the buffer, then use that array to
    // create  a string via the UTF8 encoding
    strContent = Encoding.UTF8.GetString(stream.ToArray());
    // write the number of characters in the string to the log
    AppLog.Write("Request content length: " + strContent.Length);
}

請注意,這里的幾乎所有事情都可以更有效地完成。 這不是好的示例代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM