在Visual Studio中下载文件

时间:2013-12-01 12:32:48

标签: vb.net visual-studio

我使用的是Microsoft推荐的代码。

My.Computer.Network.DownloadFile("mydlurl", "C:\jb\jb.exe")

但是这会在朋友的电脑和其他人身上引发一个未处理的例外,但是有些人可以使用它。

我尝试使用带有进度条的downloadasync,但它甚至在启动之前就冻结了。(什么也没做)

这是我的代码在某些计算机上运行未处理的异常。

If My.Computer.FileSystem.FileExists("C:\jb\jb.exe") Then
Else
    'jbinfo.Text = "Please wait as your file is being downloaded."
    My.Computer.Network.DownloadFile("http://download1580.mediafire.com/g/abcdefghijklmnop/myfile.exe", "C:\jb\jb.exe", "", "", True, 500, True) '<Maybe fix. Lets try.
End If

异常消息是:

See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
      at     Microsoft.VisualBasic.MyServices.Internal.WebClientCopy.DownloadFile(Uri address, String destinationFileName)
   at Microsoft.VisualBasic.Devices.Network.DownloadFile(Uri address, String destinationFileName, ICredentials networkCredentials, Boolean showUI, Int32 connectionTimeout, Boolean overwrite, UICancelOption onUserCancel)
   at Microsoft.VisualBasic.Devices.Network.DownloadFile(String address, String destinationFileName, String userName, String password, Boolean showUI, Int32 connectionTimeout, Boolean overwrite, UICancelOption onUserCancel)
   at Microsoft.VisualBasic.Devices.Network.DownloadFile(String address, String destinationFileName, String userName, String password, Boolean showUI, Int32 connectionTimeout, Boolean overwrite)
   at MyApp.Main.Button1_Click_1(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1 个答案:

答案 0 :(得分:0)

如果我运行此操作,我会收到ProtocolViolation,这可能是因为DownloadFile需要200 OK,然后是下载,但Mediafire会向您发送302 FOUND并重定向请求到下载此下载页面的新位置。

这是为了防止您使用MediaFire作为文件托管平台来分发文件,而人们实际上并不知道您正在使用MediaFire(理解为正确吗?)。

您将无法在此处使用wc.DownloadFile或任何其他需要文件的.NET框架方法。您需要实际与Mediafire实施正确的“握手”,这样他们就会向您发送一个新位置,然后去那里向用户展示。