Tag: stream

在进行网络I / O时是否缓冲了Stream.Read?

所以我最近做了一些工作,当有人告诉我,如果在网络流上做一个Stream.Read是通过在WebResponse上调用.NET的一个GetResponseStream获得的,或者那些是缓冲的。 他说如果你要在你正在阅读的代码中设置一个断点,你就不会停止网络流量。 我发现这很奇怪,但也希望这是真的。 这是如何运作的? 它甚至准确吗? using (Stream webResponseStream = this.webResponse.GetResponseStream()) { byte[] readBuffer = new byte[bufferSize]; int bytesRead = webResponseStream.Read(readBuffer, 0, bufferSize); while (bytesRead > 0) { bytesRead = webResponseStream.Read(readBuffer, 0, bufferSize); // If I put a breakpoint here, does network activity stop? } }

无法下载PhantomJS生成的pdf

我在将一个PhantomJS生成的文件下载到我的asp.net应用程序时遇到了一些麻烦。 我正在运行phantomJs作为服务器:文件生成正确并保存到PhantomJS文件夹中的磁盘,但在传输过程中发生了一些事情并包含在我的Http响应流中。 该文件是由浏览器下载的,但是当我尝试打开它时,文件错误,并显示无法打开的消息。 我怀疑它在流传输中被破坏了? 我想避免从文件系统中存储的位置读取pdf,而是从PhantomJS返回的响应中取出它 PhantomJS代码: page.open(url, function (status) { page.render(fullFileName); var fs = require(‘fs’); var pdfContent = fs.read(fullFileName); response.statusCode = 200; response.headers = { ‘Cache’: ‘no-cache’, ‘Content-Type’: ‘application/pdf’, ‘Connection’: ‘Keep-Alive’, ‘Content-Length’: pdfContent.length }; response.setEncoding(“binary”); response.write(pdfContent); }); ASP.NET代码: public ActionResult DownloadPdfFromUrl(ConvertionModel model) { HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(“http://localhost:8080”); ASCIIEncoding encoding = new ASCIIEncoding(); string postData = […]

Metro APP – BitmapImage to Byte 或从Web下载图像并将其转换为Byte 数组

有没有办法将BitmapImage(Windows.UI.Xaml.Media.BitmapImage)转换为Byte []数组? 我没有尝试过任何工作….另一种可能的情况(如果BitmapImage无法转换为Byte数组)是从web下载图像然后将其转换为数组… 但我不知道我怎么能这样做……如果有人有想法,那将是非常好的。 目前尝试: HttpClient http = new HttpClient(); Stream resp = await http.GetStreamAsync(“http://localhost/img/test.jpg”); var ras = new InMemoryRandomAccessStream(); await resp.CopyToAsync(ras.AsStreamForWrite()); BitmapImage bi = new BitmapImage(); bi.SetSource(ras); byte[] pixeBuffer = null; using (MemoryStream ms = new MemoryStream()) { int i = bi.PixelHeight; int i2 = bi.PixelWidth; WriteableBitmap wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight); Stream s1 […]

从ResourceStream获取图标

我有一个Icon.ico,在属性中,Build Action是“Resource”… 我想在应用程序中加载该图标.. 我做了这样的事情: Icon theIcon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(“MyNameSpace.Icon.ico”)); 没有用(它说‘null’的值对’stream’无效 。“) 我能做什么?

有没有一种方法可以将外部进程的结果流式传输到Visual Studio输出窗格?

我在VsPackage设置了一个自定义输出窗格,类似于以下内容: ///——————————————————————————– /// This property gets the custom output pane. ///——————————————————————————– private Guid _customPaneGuid = Guid.Empty; private IVsOutputWindowPane _customPane = null; private IVsOutputWindowPane customPane { get { if (_customPane == null) { IVsOutputWindow outputWindow = GetService(typeof(SVsOutputWindow)) as IVsOutputWindow; if (outputWindow != null) { // look for existing solution updater pane if (_customPaneGuid == Guid.Empty || […]

读取Global.asax中的HttpContext.Current.Response.OutputStream

我如何在Global.asax End_Request中读取HttpContext.Current.Response.OutputStream的全部内容? 我想获取发送到浏览器的内容文本? 我试着谷歌,但没有找到有用的东西。 实际上我希望这是可能的,我不需要为这个简单的要求创建一个filter或东西。 谢谢。

如何使用stringbuilder并将其转换为streamReader?

如何使用stringbuilder并将其转换为流? 所以我的stringbuilder必须转换为: StreamReader stream = ???? 更新 我尝试使用stringreader,如: StringReader sr = new StringReader(sb.ToString()); StreamReader stream = new StreamReader(sr); 但那不起作用?

处理完响应后,使用Amazon S3响应流

我正在使用亚马逊SDK,我有一个方法可以返回存储在Amazon S3服务中的对象的Stream。 它包含这样的东西: var request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName); using (var response = client.GetObject(request)) { return response.ResponseStream; } 显然,在执行此操作时,流不能从调用方法中读取,因为已经处理了请求对象,并且当完成此操作时,它将关闭流。 我不想将文件下载到MemoryStream或FileStream。 如果我不使用using子句,垃圾收集器将在某个时候处置请求对象,所以我不能不使用它。 我要问的是,有没有办法将Stream包装或复制到另一个Stream然后返回它而不必下载文件? 我正在使用.NET 3.5。 编辑:该方法inheritance自抽象类,调用方法不知道它与Amazon一起使用。 所以它有一个返回流。

在C#中使用(IDisposable obj = new …)来编写流中的代码块(例如XML)

我已经开始使用实现IDisposable的类来使用using语句在流中编写块。 这有助于保持正确的嵌套并避免丢失或错误放置的开始/结束部件。 基本上,构造函数写入块的开头(例如打开XML标记),Dispose()结束(例如关闭XML标记)。 示例是下面的UsableXmlElement(它适用于大型XML,因此内存中的LINQ to XML或XmlDocument不是选项)。 但是,这些IDisposable不实现Microsoft推荐的复杂模式,使用Destructor / Finalizer,单独的Dispose(bool)方法和GC.SuppressFinalize()。 Dispose只是简单地写出end元素,就是这样。 这有什么不妥的,或者这是保持元素正确嵌套的好方法吗? class UsableXmlElement : IDisposable { private XmlWriter _xwriter; public UsableXmlElement(string name, XmlWriter xmlWriter) { _xwriter = xmlWriter; _xwriter.WriteStartElement(name); } public void WriteAttribute(string name, T value) { _xwriter.WriteStartAttribute(name); _xwriter.WriteValue(value); _xwriter.WriteEndAttribute(); } public void WriteValue(T value) { _xwriter.WriteValue(value); } public void Dispose() { _xwriter.WriteEndElement(); } } […]

为什么要部署StreamReader会使流不可读?

我需要从开始到结束两次读取一个流。 但是下面的代码抛出了ObjectDisposedException: Cannot access a closed fileexception。 string fileToReadPath = @””; using (FileStream fs = new FileStream(fileToReadPath, FileMode.Open)) { using (StreamReader reader = new StreamReader(fs)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } fs.Seek(0, SeekOrigin.Begin); // ObjectDisposedException thrown. using (StreamReader reader = new StreamReader(fs)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } 为什么会这样? 什么是真的处置? 为什么操纵StreamReader会以这种方式影响关联的流? 期望可以多次读取可搜索流是不合逻辑的,包括几个StreamReader […]