프로그래밍언어/VB.NET

webrequest 파일다운

부산딸랑이 2013. 6. 18. 08:23

Dim uri As New UriBuilder

    uri.UserName = "xxx"
    uri.Password = "xxx"
    uri.Host = "xxx"
    uri.Path = "xxx.aspx?q=65"
   Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri.Uri), HttpWebRequest)
    request.AllowAutoRedirect = True
    request = DirectCast(WebRequest.Create(DownloadUrlIn), HttpWebRequest)
    request.Timeout = 10000
    'request.AllowWriteStreamBuffering = True
    Dim response As HttpWebResponse = Nothing
    response = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim s As Stream = response.GetResponseStream()
    'Write to disk
    Dim fs As New FileStream("c:\xxx.pdf", FileMode.Create)
    Dim read As Byte() = New Byte(255) {}
    Dim count As Integer = s.Read(read, 0, read.Length)
    While count > 0
        fs.Write(read, 0, count)
        count = s.Read(read, 0, read.Length)
    End While
    'Close everything
    fs.Close()
    s.Close()
    response.Close()

출처 : http://stackoverflow.com/questions/6778055/how-to-use-httpwebrequest-to-download-file


Public Shared Sub Opendown(ByVal URL As String, ByVal 저장경로 As String)
    Dim request As HttpWebRequest = Nothing
    request = DirectCast(WebRequest.Create(URL), HttpWebRequest)
    Dim request2 As HttpWebRequest = request
    request2.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"
    request2.ContentType = "application/x-www-form-urlencoded"
    request2.Accept = "*/*"
    request2.KeepAlive = True
    request2.ReadWriteTimeout = -1
    request2.Timeout = -1
    request2.Method = "GET"
    request2.CookieContainer = Module1.cookie
    request2 = Nothing
    Dim response2 As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
    If (response2.StatusCode = HttpStatusCode.OK) Then
        Dim responseStream As Stream = response2.GetResponseStream
        Dim reader As StreamReader = Nothing
        reader = New StreamReader(responseStream, Encoding.UTF8)
        Dim stream As New FileStream(저장경로, FileMode.Create)
        Dim buffer As Byte() = New Byte(&H100  - 1) {}
        Dim i As Integer = responseStream.Read(buffer, 0, buffer.Length)
        Do While (i > 0)
            stream.Write(buffer, 0, i)
            i = responseStream.Read(buffer, 0, buffer.Length)
        Loop
        stream.Close
        reader.Close
        response2.Close
    End If
    response2 = Nothing
End Sub

 


 


'프로그래밍언어 > VB.NET' 카테고리의 다른 글

Textbox에서 Ctrl + A 사용  (0) 2013.06.21
드래그앤드롭으포 파일명표시  (0) 2013.06.21
XML에 이미지 base64로 넣기  (0) 2013.06.14
이미지 포맷변환  (0) 2013.06.05
정규식 regex.split  (0) 2013.05.30