如何在VB中编写自动下载代码?

问题描述 投票:-1回答:1

现在我正在尝试编写一个能够从特定网站下载文件的工具,除了其他一些东西。

我目前的解决方案是,我打开网站,从Internet Explorer中下载文件,等待几秒钟,以便可以加载网站,然后发送一个按键到Internet Explorer,即ALT + S,将文件保存到标准下载位置。

但是,我的代码在应该发送击键的行中失败。你会发现它当然低于它抛出的错误。


我相信你会发现一些有用的事实:

  • 我的应用程序将是一个控制台应用程序,没有表单应用
  • 我正在使用Internet Explorer,因此我可以确定该应用程序适用于每台Windows PC
  • 我希望Internet Explorer具有默认配置,用于下载要设置为“用户提示”的文件,即它询问是否应该打开或保存文件
  • 您将在下面的代码中看到该网站是第三方网站,因此我无法直接访问服务器上托管的文件

我特别想知道的是:

  • 我能以某种方式更智能地解决这个问题吗?下载文件还有其他比我更简单的方法吗?
  • 如何让我的应用程序等待加载网站,甚至等待“打开 - 保存 - 取消”提示出现?
  • 如何让“SendKeys”部分正常工作?

最重要的是:请注意,我是一个业余爱好程序员,几周前刚刚开始使用VB。如果我错过了一些重要信息或我的代码看起来很奇怪,那么这就是为什么:)


Sub Download()

    Dim IE As Object
    IE = CreateObject("InternetExplorer.Application")

    Console.WriteLine("Start Download")
    IE.Navigate("https://toolslib.net/downloads/finish/1-adwcleaner/") 'Opens the website in Internet Explorer
    Do 'Waits for Internet Explorer to be launched before going on
        If CBool(Process.GetProcesses.Where(Function(P As Process) _ 
                 P.ProcessName = "iexplore").Count) Then
            Exit Do
        End If
        Threading.Thread.Sleep(100)
    Loop
    IE.Visible = True

    Threading.Thread.Sleep(5000) 'Some time for the website to load
    IE.SendKeys("%{S}", True)

End Sub

运行此代码时,它会引发以下错误。

Note: Line 67 is the line where IE.SendKeys("%{S}", True) stands.

Unhandled Exception: System.ArgumentException: The process {0} wasn't found.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   bei Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at MyApp.Module1.Download() in C:\Users\User\Documents\Visual Studio 2017\Projects\MyApp\MyApp\Module1.vb:Line 67.
vb.net visual-studio download sendkeys
1个回答
0
投票

我在表单应用程序中使用了WebBrowser控件,它工作得很好!

                SendKeys.Send("+{TAB}")
                System.Threading.Thread.Sleep(1000)
                SendKeys.Send("{ENTER}")
© www.soinside.com 2019 - 2024. All rights reserved.