如何将WebResponse放入内存流?

时间:2022-09-06 13:59:44

What is the best way to get a file (in this case, a .PDF, but any file will do) from a WebResponse and put it into a MemoryStream? Using .GetResponseStream() from WebResponse gets a Stream object, but if you want to convert that Stream to a specific type of stream, what do you do?

从WebResponse获取文件(在本例中为.PDF,但任何文件都可以)的最佳方法是什么,并将其放入MemoryStream?使用WebResponse中的.GetResponseStream()获取Stream对象,但如果要将该Stream转换为特定类型的流,您会怎么做?

3 个解决方案

#1


25  

There is a serious issue with SoloBold's answer that I discovered while testing it. When using it to read a file via an FtpWebRequest into a MemoryStream it intermittently failed to read the entire stream into memory. I tracked this down to Peek() sometimes returning -1 after the first 1460 bytes even though a Read() would have succeeded (the file was significantly larger than this).

SoloBold在测试时发现了一个严重的问题。当使用它通过FtpWebRequest将文件读入MemoryStream时,它间歇性地无法将整个流读入内存。我跟踪Peek()有时在前1460字节后返回-1,即使Read()成功(文件明显大于此)。

Instead I propose the solution below:

相反,我提出以下解决方案:

MemoryStream memStream;
using (Stream response = request.GetResponseStream()) {
    memStream = new MemoryStream();

    byte[] buffer = new byte[1024];
    int byteCount;
    do {
        byteCount = stream.Read(buffer, 0, buffer.Length);
        memStream.Write(buffer, 0, byteCount);
    } while (byteCount > 0);
}

// If you're going to be reading from the stream afterwords you're going to want to seek back to the beginning.
memStream.Seek(0, SeekOrigin.Begin);

// Use memStream as required

#2


0  

Copied this from the web a year or so ago.

大约一年前从网上复制了这个。

//---------- Start HttpResponse
if(objHttpWebResponse.StatusCode == HttpStatusCode.OK)
    {
        //Get response stream
        objResponseStream = objHttpWebResponse.GetResponseStream();

        //Load response stream into XMLReader
        objXMLReader = new XmlTextReader(objResponseStream);

        //Declare XMLDocument
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(objXMLReader);

        //Set XMLResponse object returned from XMLReader
        XMLResponse = xmldoc;

        //Close XMLReader
        objXMLReader.Close();
    }

    //Close HttpWebResponse
    objHttpWebResponse.Close();
}

#3


-3  

I found the following at http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/eeeefd81-8800-41b2-be63-71acdaddce0e/

我在http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/eeeefd81-8800-41b2-be63-71acdaddce0e/找到以下内容

    Dim request As WebRequest
    Dim response As WebResponse = Nothing
    Dim s As Stream = Nothing
    Dim fs As FileStream = Nothing
    Dim file As MemoryStream = Nothing

    Dim uri As New Uri(String.Format("http://forums.microsoft.com/forums/ShowPost.aspx?PostID=2992978&SiteID=1"))
    request = WebRequest.Create(uri)
    request.Timeout = 10000
    response = request.GetResponse
    s = response.GetResponseStream

    '2 - Receive file as memorystream
    Dim read(256) As Byte
    Dim count As Int32 = s.Read(read, 0, read.Length)
    File = New MemoryStream
    Do While (count > 0)
        File.Write(read, 0, count)
        count = s.Read(read, 0, read.Length)
    Loop
    File.Position = 0
    'Close responsestream
    s.Close()
    response.Close()

    '3 - Save file
    fs = New FileStream("c:\test.html", FileMode.CreateNew)
    count = file.Read(read, 0, read.Length)
    Do While (count > 0)
        fs.Write(read, 0, count)
        count = file.Read(read, 0, read.Length)
    Loop
    fs.Close()
    File.Close()

#1


25  

There is a serious issue with SoloBold's answer that I discovered while testing it. When using it to read a file via an FtpWebRequest into a MemoryStream it intermittently failed to read the entire stream into memory. I tracked this down to Peek() sometimes returning -1 after the first 1460 bytes even though a Read() would have succeeded (the file was significantly larger than this).

SoloBold在测试时发现了一个严重的问题。当使用它通过FtpWebRequest将文件读入MemoryStream时,它间歇性地无法将整个流读入内存。我跟踪Peek()有时在前1460字节后返回-1,即使Read()成功(文件明显大于此)。

Instead I propose the solution below:

相反,我提出以下解决方案:

MemoryStream memStream;
using (Stream response = request.GetResponseStream()) {
    memStream = new MemoryStream();

    byte[] buffer = new byte[1024];
    int byteCount;
    do {
        byteCount = stream.Read(buffer, 0, buffer.Length);
        memStream.Write(buffer, 0, byteCount);
    } while (byteCount > 0);
}

// If you're going to be reading from the stream afterwords you're going to want to seek back to the beginning.
memStream.Seek(0, SeekOrigin.Begin);

// Use memStream as required

#2


0  

Copied this from the web a year or so ago.

大约一年前从网上复制了这个。

//---------- Start HttpResponse
if(objHttpWebResponse.StatusCode == HttpStatusCode.OK)
    {
        //Get response stream
        objResponseStream = objHttpWebResponse.GetResponseStream();

        //Load response stream into XMLReader
        objXMLReader = new XmlTextReader(objResponseStream);

        //Declare XMLDocument
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(objXMLReader);

        //Set XMLResponse object returned from XMLReader
        XMLResponse = xmldoc;

        //Close XMLReader
        objXMLReader.Close();
    }

    //Close HttpWebResponse
    objHttpWebResponse.Close();
}

#3


-3  

I found the following at http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/eeeefd81-8800-41b2-be63-71acdaddce0e/

我在http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/eeeefd81-8800-41b2-be63-71acdaddce0e/找到以下内容

    Dim request As WebRequest
    Dim response As WebResponse = Nothing
    Dim s As Stream = Nothing
    Dim fs As FileStream = Nothing
    Dim file As MemoryStream = Nothing

    Dim uri As New Uri(String.Format("http://forums.microsoft.com/forums/ShowPost.aspx?PostID=2992978&SiteID=1"))
    request = WebRequest.Create(uri)
    request.Timeout = 10000
    response = request.GetResponse
    s = response.GetResponseStream

    '2 - Receive file as memorystream
    Dim read(256) As Byte
    Dim count As Int32 = s.Read(read, 0, read.Length)
    File = New MemoryStream
    Do While (count > 0)
        File.Write(read, 0, count)
        count = s.Read(read, 0, read.Length)
    Loop
    File.Position = 0
    'Close responsestream
    s.Close()
    response.Close()

    '3 - Save file
    fs = New FileStream("c:\test.html", FileMode.CreateNew)
    count = file.Read(read, 0, read.Length)
    Do While (count > 0)
        fs.Write(read, 0, count)
        count = file.Read(read, 0, read.Length)
    Loop
    fs.Close()
    File.Close()