0

我有启动 ActiveX 以下载文件的 html。

我希望通过 C#/.NET 完成,而不是使用 Internet Explorer 下载文件

HTML 如下所示:

<HTML>
    <HEAD>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <OBJECT ID="o" CLASSID="CLSID:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" CODEBASE="https://YYYYYY/grTransferCtrl.cab#version=5,0,0,32">
        </OBJECT>
        <SCRIPT LANGUAGE=VBScript FOR=window EVENT="onLoad()">

            On Error Resume Next

            o.Persist "AAA"
            o.Persist "BBB"
            o.Persist "CCC"

            bRetVal = o.Launch()
            If(Err.Number > 0 Or bRetVal = False) Then 
            msgbox "There was an error launching File Transfer Manager.",0,""
            End if

        </SCRIPT>
    </HEAD>
<BODY></BODY>


如何直接从我的 C# 应用程序启动下载?

4

2 回答 2

0

您要查找的关键字是 ` WebRequest '

以下将大致完成您想要的操作:

var request = WebRequest.Create("https://YYYYYY/grTransferCtrl.cab#version=5,0,0,32");

//This is where you may need to add cookies and other header data for this to work.
//I would use fiddler to try and inspect the http requests sent by the control for this
//information
request.UseDefaultCredentials = true;
var response = request.GetResponse();

using (var file= response.GetResponseStream())
{
    //do something with the stream? save it?
}   
response.Close();

作为替代方案,您可以在应用程序中创建 ActiveX 控件并直接使用它。Web 上有大量资源可用于将 ActiveX 控件添加到 .NET 表单应用程序,只是不知道此特定控件对其环境做出的哪些假设可能会阻止这种情况发生。

于 2012-07-16T19:55:25.190 回答
0

您可以通过使用 System.Net.WebClient 来实现这一点

Example:

public class Downloader
{
  public void DownloadFile()
  {
     using(WebClient webClient = new WebClient())
     {
         webClient.DownloadFile("http://www.stackoverflow.com/stacks.txt", @"c:\stacks.txt");
     }
  }
}
于 2012-07-16T20:10:18.503 回答