繁体   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