Alternative to inet & webbrowser control to retrie

2019-06-09 00:08发布

问题:

I would like to download only the HTML code in my VB6 program. Webbrowser control is good but it hangs and also it has issues such as needing to disable the JavaScript, pic, etc in order to get just the HTML

The Inet control is better, but it is "buggy"

Is there any other control ?

回答1:

If you only want to download the HTML of a page, you can easily use Winsock control.

Private Sub Form_Load()
  Winsock1.Connect "stackoverflow.com", 80
End Sub

Private Sub Winsock1_Close()
  Winsock1.Close
End Sub

Private Sub Winsock1_Connect()
  Winsock1.SendData "GET /questions/8624871/vb6-alternative-to-inet-webbrowser-control HTTP/1.1" & vbCrLf & "Host: stackoverflow.com" & vbCrLf & vbCrLf
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim s As String

  Winsock1.GetData s, vbString
  RichTextBox1.Text = RichTextBox1.Text & s
End Sub


回答2:

My suggestion is to host WebKit.NET in your VB 6 application just like you would any other .NET control.

Although it is a .NET control, which means that it won't work natively out-of-the-box with VB 6, it is possible to use controls developed in .NET with a VB 6 application. Microsoft provides the Interop Forms Toolkit as an interoperability mechanism between the two languages.

Essentially, you'll use one of the .NET languages (it doesn't have to be VB.NET; you could also use C#) to create an ActiveX DLL containing your UserControl and register it for COM interop. Then, you can add this ActiveX control to your VB 6 project and hook up to handle the events it raises.

You can find a more complete sample on how to do this here on CodeProject, or here on CodeGuru.

Indeed this approach is not going to be trivial to implement. But I suspect that it's your only alternative to the bundled WebBrowser control, considering that IE was pretty much dominating the browser market back when VB 6 was popular and no one is developing new controls for VB 6 anymore.



回答3:

You don't need API calls, you don't need WinSock, you don't need Inet, you don't need WebKit interop. Just do it the easy way, using native VB6 code. Here's an excellent article by Karl Peterson with sample code.



回答4:

This is good code, and it works even in ASP, VBScript.

Function GetHTMLCode(strURL) As String
    Dim strReturn                   ' As String
    Dim objHTTP                     ' As MSXML.XMLHTTPRequest
    If Len(strURL)=0 Then EXIT FUNCTION
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    objHTTP.open "GET", strURL,False
    objHTTP.send                    'Get it.
    strReturn =objHTTP.responseText
    Set objHTTP = Nothing 
    GetHTMLCode = strReturn 
End Function

Now call this function like this (always write with protocol):

Msgbox GetHTMLCode("http://www.stackoverflow.com")


标签: vb6