Automate picture downloads from website with authentication

后端 未结 2 1505
失恋的感觉
失恋的感觉 2021-01-22 00:43

My intention is to automate the downloading of all pictures in a website that requires a login (a web-form based login I think)

The website: http://www.cgwallpapers.com<

2条回答
  •  借酒劲吻你
    2021-01-22 01:29

    Private Function DownloadImage() As String
        Dim remoteImgPath As String = "http://www.cgwallpapers.com/members/viewwallpaper.php?id=1764&res=1920x1080"
        Dim remoteImgPathUri As New Uri(remoteImgPath)
        Dim remoteImgPathWithoutQuery As String = remoteImgPathUri.GetLeftPart(UriPartial.Path)
        Dim fileName As String = Path.GetFileName(remoteImgPathWithoutQuery)
        Dim localPath As String = Convert.ToString(AppDomain.CurrentDomain.BaseDirectory + "LocalFolder\Images\Originals\") & fileName
        Dim webClient As New WebClient()
        webClient.DownloadFile(remoteImgPath, localPath)
        Return localPath
    End Function
    

    I threw this together I think its the right direction.

    Try

            Dim theFile As String = "c:\wallpaper.jpg"
    
            Dim fileName As String
    
            fileName = Path.GetFileName(theFile)
    
    
    
            Dim ms = New MemoryStream(File.ReadAllBytes(theFile))
    
    
    
            Dim dataLengthToRead As Long = ms.Length
            Dim blockSize As Integer = If(dataLengthToRead >= 5000, 5000, CInt(dataLengthToRead))
            Dim buffer As Byte() = New Byte(dataLengthToRead - 1) {}
    
    
            Response.Clear()
            Response.ClearContent()
            Response.ClearHeaders()
            Response.BufferOutput = True
    
    
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
            Response.AddHeader("Content-Disposition", "inline; filename=" + fileName)
    
            Response.AddHeader("Content-Length", blockSize.ToString())
            Response.ContentType = "image/JPEG"
    
    
    
            While dataLengthToRead > 0 AndAlso Response.IsClientConnected
                Dim lengthRead As Int32 = ms.Read(buffer, 0, blockSize)
                Response.OutputStream.Write(buffer, 0, lengthRead)
                Response.Flush()
                dataLengthToRead = dataLengthToRead - lengthRead
            End While
    
    
    
    
            Response.Flush()
            Response.Close()
    
    
        Catch ex As Exception
    
        End Try
    

提交回复
热议问题