gpt4 book ai didi

vb.net - 如何使用HttpWebRequest下载文件

转载 作者:行者123 更新时间:2023-12-04 15:26:25 28 4
gpt4 key购买 nike

尝试在代码中下载文件。

当前代码:

  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()

运行此代码并检查 response.ResponseUri 表明我被重定向回登录页面而不是 pdf 文件。

出于某种原因,它没有授权访问我在 uri 中发送用户名和密码时可能会遗漏什么?谢谢你的帮助

最佳答案

你不需要所有的代码来从网上下载文件
只需使用 WebClient类及其 DownloadFile方法

关于vb.net - 如何使用HttpWebRequest下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6778055/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com