VB.NET上载问题-必须写入ContentLength字节

VB.NET上载问题-必须写入ContentLength字节,vb.net,upload,httpwebrequest,httpwebresponse,system.net,Vb.net,Upload,Httpwebrequest,Httpwebresponse,System.net,使用下面的函数将视频文件上载到web服务器时,我遇到以下异常。我使用这个功能,因为我需要内存使用量低,因为上传的视频文件超过150MB 引发的错误是: 在调用[Begin]GetResponse之前,必须将ContentLength字节写入请求流 我已经看过代码好几次了&根本没有注意到哪里出了错&可能只是需要第二双眼睛来发现我的错误 功能: Friend Function LowMemoryUploader(ByVal FilePath As String) As String

使用下面的函数将视频文件上载到web服务器时,我遇到以下异常。我使用这个功能,因为我需要内存使用量低,因为上传的视频文件超过150MB

引发的错误是: 在调用[Begin]GetResponse之前,必须将ContentLength字节写入请求流

我已经看过代码好几次了&根本没有注意到哪里出了错&可能只是需要第二双眼睛来发现我的错误

功能:

Friend Function LowMemoryUploader(ByVal FilePath As String) As String
        ServicePointManager.ServerCertificateValidationCallback = (Function(sender, certificate, chain, sslPolicyErrors) True)
        Dim oUri As New Uri("http://mysite.com/upload.php")
        Dim NowTime As String = DateTime.Now.Ticks.ToString().Substring(0, 14)
        Dim strBoundary As String = "-----------------------------" & NowTime
        ' Set Filename
        Dim FileName As String = FilePath.Split(CChar("\")).Last()
        ' The trailing boundary string
        Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & strBoundary & vbCr & vbLf)
        ' The post message header
        Dim sb As New StringBuilder()
        ' Add Variables
        sb.Append(strBoundary & vbCrLf & "Content-Disposition: form-data; name=""upload""" & vbCrLf & vbCrLf & "1" & vbCrLf)
        sb.Append(strBoundary & vbCrLf & "Content-Disposition: form-data; name=""upload_file""; filename=""" & FileName & """" & vbCrLf)
        sb.Append("Content-Type: video/" & FilePath.Split(".").Last())
        sb.Append(vbCrLf & vbCrLf)
        ' Set Header Bytes
        Dim strPostHeader As String = sb.ToString()
        Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(strPostHeader)
        ' The WebRequest
        Dim oWebrequest As HttpWebRequest = DirectCast(WebRequest.Create(oUri), HttpWebRequest)
        ' Set Request Settings
        System.Net.ServicePointManager.Expect100Continue = False
        oWebrequest.Method = "POST"
        oWebrequest.UserAgent = Useragent
        oWebrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        oWebrequest.ContentType = "multipart/form-data; boundary=---------------------------" & NowTime
        oWebrequest.AllowAutoRedirect = True
        oWebrequest.Timeout = 600000
        oWebrequest.CookieContainer = cookies
        ' This is important, otherwise the whole file will be read to memory anyway...
        oWebrequest.AllowWriteStreamBuffering = False
        ' Get a FileStream and set the final properties of the WebRequest
        Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read)
        Dim length As Long = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length
        oWebrequest.ContentLength = length
        Dim oRequestStream As Stream = oWebrequest.GetRequestStream()
        ' Write the post header
        oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
        ' Stream the file contents in small pieces (4096 bytes, max).
        Dim buffer As Byte() = New Byte(1096) {}
        Dim bytesRead As Integer = oFileStream.Read(buffer, 0, buffer.Length)
        While bytesRead <> 0
            bytesRead = oFileStream.Read(buffer, 0, buffer.Length)
            oRequestStream.Write(buffer, 0, bytesRead)
        End While
        oFileStream.Close()
        ' Add the trailing boundary
        oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
        Dim oWResponse As WebResponse = oWebrequest.GetResponse()
        Dim s As Stream = oWResponse.GetResponseStream()
        Dim sr As New StreamReader(s)
        Dim sReturnString As String = sr.ReadToEnd()
        ' Clean up
        oRequestStream.Close()
        s.Close()
        sr.Close()
        Return sReturnString
    End Function

这部分看起来不对劲:

Dim bytesRead As Integer = oFileStream.Read(buffer, 0, buffer.Length)
While bytesRead <> 0
    bytesRead = oFileStream.Read(buffer, 0, buffer.Length)
    oRequestStream.Write(buffer, 0, bytesRead)
End While
注意,Dim bytesRead As Integer=oFileStream.Readbuffer,0,buffer.Length进行初始读取,但它会立即在循环中被覆盖。因此,向oRequestStream写入的块比读取的块少一个