Visual Basic程序化下载Google Drive加密RAR文件遇阻求解决方案
Google Drive带病毒提示的文件VB下载解决方案
问题背景
我用Visual Basic编写的程序需从Google Drive下载带密码保护的RAR压缩包,此前两段代码可正常运行,但因Google规则变更失效。目标文件约1.5MB,将链接放入浏览器会打开提示页面,显示“无法检测文件是否携带病毒”等内容,点击“仍要下载”按钮可正常下载。此前通过在链接后添加&confirm=yes解决问题,但该方案现已失效,现寻求不使用Google API的简洁优雅解决方案。
此前使用的代码
v1
Dim fileReader As New WebClient() Dim fileAddress = "https://drive.google.com/my_file_direct_link" fileReader.DownloadFile(fileAddress, SaveFileDialog1.FileName)
v2
Dim fileReader As New WebClient() Dim fileAddress = "https://drive.google.com/my_file_direct_link&confirm=yes" fileReader.DownloadFile(fileAddress, SaveFileDialog1.FileName)
解决方案
当前Google的验证机制已更新,动态生成确认参数,需先模拟浏览器请求获取真实下载链接,再执行下载:
Imports System.Net Imports System.Text.RegularExpressions Public Sub DownloadGDriveFile(originalUrl As String, savePath As String) Dim webClient As New WebClient() ' 模拟浏览器请求头,规避非浏览器请求拦截 webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") ' 第一步:获取包含真实下载链接的页面内容 Dim pageContent As String = webClient.DownloadString(originalUrl) ' 正则提取带动态确认参数的真实下载链接 Dim regex As New Regex("href=""(uc\?export=download[^""]+)""") Dim match As Match = regex.Match(pageContent) If match.Success Then Dim realDownloadUrl As String = "https://drive.google.com/" & match.Groups(1).Value ' 第二步:执行文件下载 webClient.DownloadFile(realDownloadUrl, savePath) Else Throw New Exception("无法提取真实下载链接") End If End Sub
使用示例
' 替换为你的文件链接和保存路径 DownloadGDriveFile("https://drive.google.com/your_file_link", SaveFileDialog1.FileName)
注意事项
- 确保项目引用
System.Net和System.Text.RegularExpressions命名空间 - 可根据实际浏览器版本更新
User-Agent值,提升兼容性 - 若Google后续再次调整验证逻辑,可能需要微调正则表达式匹配规则
内容的提问来源于stack exchange,提问作者equ
相关产品推荐
相关产品推荐

