将GetResponseStream写入文件需要很长时间

时间:2012-12-21 09:11:53

标签: performance httpwebrequest ssrs-2008 httpwebresponse getresponsestream

我已经成为一名SSRS Bulk记者,使用HTTPWebRequest和HTTPWebResponse查询来自SSRS的报告,以获取流并将其作为文件保存到PDF。

然而,客户抱怨这需要很长时间。我测试了它,这个函数需要+ - 15.5秒。如果我在GetResponseStream和文件编写器之间放置一个中断,并且我等待大约3秒然后踩到下一个部分,它会缩短总时间4秒?

任何人都可以解释这个/或者提出一些建议,让它更快一点吗?

这是功能:

 Public Function ExportReport(ByVal QueryStringParameters As String, ByVal FileName As String) As String
    Dim PDFName As String = ReportFolderPath & FileName & ".pdf"
    'Create a http request or connecting to the report server.
    Dim ReportHTTPRequest As HttpWebRequest = Nothing
    'Create a http response to catch the data stream returned from the http request.
    Dim ReportHTTPResponse As HttpWebResponse = Nothing
    'Create a stream to read the binary data from the http reponse.
    Dim ReportStream As Stream = Nothing
    Dim ReportFileStream As FileStream = Nothing
    'Create an array of bytes to get the binary data from the stream.
    Dim ReportBytes As Byte()
    Dim ReportBuffer As Integer = 204800
    Dim ReportBytesRead As Integer = 0


    'Create a webrequest to get the report with all the report parameters included.
    ReportHTTPRequest = WebRequest.Create(ReportServerURL & QueryStringParameters)
    ReportHTTPRequest.Credentials = CredentialCache.DefaultCredentials
    'Get the response from the request.

    ReportHTTPResponse = ReportHTTPRequest.GetResponse()

    'Read the binary stream from the http response.
    ReportStream = ReportHTTPResponse.GetResponseStream()

    ReportBytes = New Byte(ReportBuffer) {}
    ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
    ReportFileStream = New FileStream(PDFName, FileMode.Create)
    Do While ReportStream.CanRead And ReportBytesRead > 0
        ReportFileStream.Write(ReportBytes, 0, ReportBytesRead)
        ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
    Loop

    ReportHTTPResponse.Close()
    ReportStream.Close()
    ReportFileStream.Close()

    Return PDFName
End Function

1 个答案:

答案 0 :(得分:0)

一个对我有用的神奇之处,尝试缓冲区大小为32768