2016-07-25 4 views
0

나는 다음과 같이 도움을 찾고 있어요에 다운로드 파일 : 내 Windows에서VB.NET의 HttpWebRequest 덩어리

내가 제어 원격 서버에있는 파일을 다운로드하고있어, Forms 응용 프로그램. 아래에 내 코드 :

Public Shared Function DownloadFileWithPOST(ByVal httpPath As String, ByVal storePath As String, postdata As String) As String 
    Try 
     Dim theResponse As HttpWebResponse 
     Dim theRequest As HttpWebRequest 
     Dim postdatabytes As Byte() 

     theRequest = HttpWebRequest.Create(httpPath) 


     If My.Settings.ProxyURL <> "" Then 
      Dim prx As New WebProxy(My.Settings.ProxyURL) 
      theRequest.Proxy = prx 
     End If 


     ' theRequest.Timeout = My.Settings.RequestTimeout 

     postdatabytes = System.Text.Encoding.UTF8.GetBytes(postdata) 
     theRequest.Method = "POST" 
     theRequest.ContentType = "application/x-www-form-urlencoded" 
     theRequest.ContentLength = postdatabytes.Length 

     Using stream = theRequest.GetRequestStream() 
      stream.Write(postdatabytes, 0, postdatabytes.Length) 
     End Using 



     theResponse = theRequest.GetResponse 


     Dim length As Double = theResponse.ContentLength 




     Dim writeStream As New IO.FileStream(storePath, IO.FileMode.Create) 


     Dim nRead As Integer 

     Dim readBytes(4095) As Byte 
     Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096) 



     Do Until bytesread = 0 



      'speedtimer.Start() 


      nRead += bytesread 



      writeStream.Write(readBytes, 0, bytesread) 


      bytesread = theResponse.GetResponseStream.Read(readBytes, 0, 4096) 
     Loop 

     'Close the streams 
     theResponse.GetResponseStream.Close() 
     writeStream.Close() 


     Return "OK" 
    Catch ex As Exception 
     Return ex.Message 
    End Try 
End Function 

문제 다운로드가 지정된 시간 (타임 아웃 설정에 주석 라인)에 완료되지 않은 경우 우리는 시간 제한 예외를 얻고 있다는 것입니다. 다운로드가 청크 분할 된 경우 드라이브 크기가 커지는 것을 볼 수 있습니다. 그러나이 대신 응답이 완전히 끝나면 파일이 나타납니다.

서버 쪽에서는 response.outputstream.write 메소드를 사용하여 청크를 보내고 있습니다.

저속 연결시 대용량 파일에 시간 초과가 발생하지 않도록하려면 어떻게해야합니까?

답변

0

POST 요청을 보낸 경우에만 발생하는 것으로 나타났습니다. params를 URL querystring의 일부로 변경했으며 청크 다운로드가 이제 작동합니다.

Public Shared Function DownloadFileWithQueryString(ByVal httpPath As String, ByVal storePath As String, querystring As String) As String 
    Try 
     Dim theResponse As HttpWebResponse 
     Dim theRequest As HttpWebRequest 
     Dim fullurl As String = httpPath & "?" & querystring 
     theRequest = HttpWebRequest.Create(fullurl) 


     If My.Settings.ProxyURL <> "" Then 
      Dim prx As New WebProxy(My.Settings.ProxyURL) 
      theRequest.Proxy = prx 
     End If 




     theResponse = theRequest.GetResponse 

     Dim length As Double = theResponse.ContentLength 




     Dim writeStream As New IO.FileStream(storePath, IO.FileMode.Create) 


     Dim nRead As Integer 

     Dim readBytes(4095) As Byte 
     Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096) 



     Do Until bytesread = 0 



      'speedtimer.Start() 


      nRead += bytesread 



      writeStream.Write(readBytes, 0, bytesread) 


      bytesread = theResponse.GetResponseStream.Read(readBytes, 0, 4096) 
     Loop 

     'Close the streams 
     theResponse.GetResponseStream.Close() 
     writeStream.Close() 


     Return "OK" 
    Catch ex As Exception 
     Return ex.Message 
    End Try 
End Function