如何在VB6中使Internet Explorer不可见?

时间:2023-01-27 11:25:55

Doing like so:

这样做:

Shell ("C:\Program Files\Internet Explorer\iexplore.exe -embedding http://www.websiteurl.com")

Shell(“C:\ Program Files \ Internet Explorer \ iexplore.exe -embedding http://www.websiteurl.com”)

Doesn't work how I need it as I essentially need it to be able to redirect and prompt a user to download a file. Any ideas?

不起作用我不需要它,因为我基本上需要它能够重定向并提示用户下载文件。有任何想法吗?

6 个解决方案

#1


5  

Internet Explorer exposes a COM accessible interface you can use. If you really have to. I'd recommend against it - its comparatively slow, error-prone, cumbersome and resource-intensive.

Internet Explorer公开了可以使用的COM可访问接口。如果你真的需要。我建议反对它 - 它相对缓慢,容易出错,繁琐且资源密集。

What solves your problem more elegantly is using WinHTTPRequest. In your Project, reference "Microsoft WinHTTP Services, version 5.1", and then go on like this:

更优雅地解决您的问题的是使用WinHTTPRequest。在您的项目中,引用“Microsoft WinHTTP Services,版本5.1”,然后继续这样:

Dim HttpRequest As New WinHttp.WinHttpRequest
Dim TargetUrl As String
Dim TargetFile As String
Dim FileNum As Integer

TargetFile = "C:\foo.doc"

TargetUrl = "http://www.websiteurl.com"
HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
HttpRequest.Send

If HttpRequest.Status = 302 Then

  TargetUrl = HttpRequest.GetResponseHeader("Location")
  HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
  HttpRequest.Send

  If HttpRequest.Status = "200" Then

    FileNum = FreeFile
    Open TargetFile For Binary As #FileNum
    Put #FileNum, 1, HttpRequest.ResponseBody
    Close FileNum 

    Debug.Print "Successfully witten " & TargetFile
  Else
    Debug.Print "Download failed. Received HTTP status: " & HttpRequest.Status
  End If
Else
  Debug.Print "Expected Redirect. Received HTTP status: " & HttpRequest.Status
End If

Hard-coding "C:\foo.doc" does of course not make much sense. I'd use the file name the server supplies in the response headers ("Content-Type" or "Content-Disposition", depending on what you expect).

硬编码“C:\ foo.doc”当然没有多大意义。我将使用服务器在响应标头中提供的文件名(“Content-Type”或“Content-Disposition”,具体取决于您的期望)。

#2


1  

There are a couple of things you could do.

你可以做几件事。

  • Use an external program like wget to get the file instead of IE. You can get wget for free at http://www.cygwin.com with the cygnus tools. It's GPL, so just watch out if you have a commercial product.

    使用像wget这样的外部程序来获取文件而不是IE。您可以使用cygnus工具在http://www.cygwin.com免费获得wget。这是GPL,所以请注意你是否有商业产品。

  • Write a little .NET program that uses the HttpWebRequest class to get the file and shell out to that program instead of IE. I don't think you're going to have a lot of luck shelling out to IE itself. Sounds like a, to paraphrase Steve Jobs, "bag of hurt".

    编写一个使用HttpWebRequest类的.NET程序来获取文件和shell而不是IE。我不认为你会给IE本身带来很多运气。用史蒂夫乔布斯的话来说,听起来像一个“受伤的袋子”。

#3


0  

If all you are trying to do is download a file, you can use URLDownloadToFile.

如果你要做的只是下载文件,你可以使用URLDownloadToFile。

#4


0  

The Internet Explorer interface is exposed to ActiveX via the WebBrowser control (contained in %systemroot%\system32\shlwapi.dll). While it may not be very elegant, you could easily place the control somewhere off the visible area of the form.

Internet Explorer界面通过WebBrowser控件(包含在%systemroot%\ system32 \ shlwapi.dll中)暴露给ActiveX。虽然它可能不是很优雅,但您可以轻松地将控件放在窗体可见区域的某个位置。

The control is very simple to use.

该控件使用起来非常简单。

#5


0  

Your best bet is creating separate download application using some .NET http object in order to download the file. I'd recommend WebClient.

最好的办法是使用一些.NET http对象创建单独的下载应用程序以下载文件。我推荐WebClient。

If you really gotta stick to VB6, I'm sure you can use some basic socket work in order to download the file directly.

如果你真的要坚持使用VB6,我相信你可以使用一些基本的套接字工作来直接下载文件。

#6


0  

Another option besides the URLDownloadToFile API call suggested by Glomek is to use the AsyncRead method built into VB6.

除了Glomek建议的URLDownloadToFile API调用之外的另一个选择是使用VB6中内置的AsyncRead方法。

#1


5  

Internet Explorer exposes a COM accessible interface you can use. If you really have to. I'd recommend against it - its comparatively slow, error-prone, cumbersome and resource-intensive.

Internet Explorer公开了可以使用的COM可访问接口。如果你真的需要。我建议反对它 - 它相对缓慢,容易出错,繁琐且资源密集。

What solves your problem more elegantly is using WinHTTPRequest. In your Project, reference "Microsoft WinHTTP Services, version 5.1", and then go on like this:

更优雅地解决您的问题的是使用WinHTTPRequest。在您的项目中,引用“Microsoft WinHTTP Services,版本5.1”,然后继续这样:

Dim HttpRequest As New WinHttp.WinHttpRequest
Dim TargetUrl As String
Dim TargetFile As String
Dim FileNum As Integer

TargetFile = "C:\foo.doc"

TargetUrl = "http://www.websiteurl.com"
HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
HttpRequest.Send

If HttpRequest.Status = 302 Then

  TargetUrl = HttpRequest.GetResponseHeader("Location")
  HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
  HttpRequest.Send

  If HttpRequest.Status = "200" Then

    FileNum = FreeFile
    Open TargetFile For Binary As #FileNum
    Put #FileNum, 1, HttpRequest.ResponseBody
    Close FileNum 

    Debug.Print "Successfully witten " & TargetFile
  Else
    Debug.Print "Download failed. Received HTTP status: " & HttpRequest.Status
  End If
Else
  Debug.Print "Expected Redirect. Received HTTP status: " & HttpRequest.Status
End If

Hard-coding "C:\foo.doc" does of course not make much sense. I'd use the file name the server supplies in the response headers ("Content-Type" or "Content-Disposition", depending on what you expect).

硬编码“C:\ foo.doc”当然没有多大意义。我将使用服务器在响应标头中提供的文件名(“Content-Type”或“Content-Disposition”,具体取决于您的期望)。

#2


1  

There are a couple of things you could do.

你可以做几件事。

  • Use an external program like wget to get the file instead of IE. You can get wget for free at http://www.cygwin.com with the cygnus tools. It's GPL, so just watch out if you have a commercial product.

    使用像wget这样的外部程序来获取文件而不是IE。您可以使用cygnus工具在http://www.cygwin.com免费获得wget。这是GPL,所以请注意你是否有商业产品。

  • Write a little .NET program that uses the HttpWebRequest class to get the file and shell out to that program instead of IE. I don't think you're going to have a lot of luck shelling out to IE itself. Sounds like a, to paraphrase Steve Jobs, "bag of hurt".

    编写一个使用HttpWebRequest类的.NET程序来获取文件和shell而不是IE。我不认为你会给IE本身带来很多运气。用史蒂夫乔布斯的话来说,听起来像一个“受伤的袋子”。

#3


0  

If all you are trying to do is download a file, you can use URLDownloadToFile.

如果你要做的只是下载文件,你可以使用URLDownloadToFile。

#4


0  

The Internet Explorer interface is exposed to ActiveX via the WebBrowser control (contained in %systemroot%\system32\shlwapi.dll). While it may not be very elegant, you could easily place the control somewhere off the visible area of the form.

Internet Explorer界面通过WebBrowser控件(包含在%systemroot%\ system32 \ shlwapi.dll中)暴露给ActiveX。虽然它可能不是很优雅,但您可以轻松地将控件放在窗体可见区域的某个位置。

The control is very simple to use.

该控件使用起来非常简单。

#5


0  

Your best bet is creating separate download application using some .NET http object in order to download the file. I'd recommend WebClient.

最好的办法是使用一些.NET http对象创建单独的下载应用程序以下载文件。我推荐WebClient。

If you really gotta stick to VB6, I'm sure you can use some basic socket work in order to download the file directly.

如果你真的要坚持使用VB6,我相信你可以使用一些基本的套接字工作来直接下载文件。

#6


0  

Another option besides the URLDownloadToFile API call suggested by Glomek is to use the AsyncRead method built into VB6.

除了Glomek建议的URLDownloadToFile API调用之外的另一个选择是使用VB6中内置的AsyncRead方法。