Obtaining a web page's title

I'd like to get the titles of web pages, and would prefer not to do this by loading all the web pages into a webbrowser, because that seems a bit heavy handed. In VB6 I used some APIs to achieve this, but I'm not having any luck in getting it to work in .net. Here's the code I'm trying:

Module Module2

Private Const INTERNET_FLAG_RELOAD = &H80000000

Private Declare Auto Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Integer, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Integer

Private Declare Auto Function InternetReadFile Lib "wininet" (ByVal hFile As Integer, ByVal sBuffer As String, ByVal lNumBytesToRead As Integer, ByVal lNumberofBytesRead As Integer) As Integer

Private Const INTERNET_OPEN_TYPE_DIRECT = 1

Declare Auto Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer

Friend Declare Auto Function InternetCloseHandle Lib "wininet" (ByRef hInet) As Integer

Friend hOpen As Integer

Friend Sub ShowTitle()

hOpen = InternetOpen("App1", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)

Dim sBuffer As String = Space(1000) '1000 characters must surely be enough.

Dim hFile As Integer

Dim Ret As Integer

Dim sPath As String = "http://www.google.co.uk/"

hFile = InternetOpenUrl(hOpen, sPath, vbNullString, 0&, INTERNET_FLAG_RELOAD, 0&)

InternetReadFile(hFile, sBuffer, 1000, Ret)

InternetCloseHandle(hFile)

Debug.Print(sBuffer)

Dim t1 As Long

Dim t2 As Long

t1 = InStr(sBuffer, "<TITLE>") + 7

t2 = InStr(sBuffer, "</TITLE>")

If t1 <> 0 And t2 <> 0 Then Debug.Print(Mid$(sBuffer, t1, t2 - t1))

InternetCloseHandle(hOpen)

End Sub

End Module


 

But sBuffer doesn't get filled. Any thoughts


Answer this question

Obtaining a web page's title

  • mimino

    I'm confused. Are you asking why it doesn't work Cannot do in .Net How to in VFP Anyway mostly based on your codes this works under VFP (title was "Google":)

    #Define INTERNET_FLAG_RELOAD 0x80000000
    #Define INTERNET_OPEN_TYPE_DIRECT 1

    Declare Integer InternetOpenUrl In wininet.Dll ;
      integer hInternetSession, String lpszUrl, String lpszHeaders,;
      integer dwHeadersLength, Integer dwFlags, Integer dwContext

    Declare Integer InternetReadFile In wininet.Dll ;
      integer hFile, ;
      string @ sBuffer, ;
      integer lNumBytesToRead, Integer @ lNumberofBytesRead


    Declare Integer InternetOpen In wininet.Dll ;
      string sAgent, ;
      integer lAccessType, ;
      string sProxyName,;
      string sProxyBypass, ;
      integer lFlags

    Declare Integer InternetCloseHandle In wininet.Dll Integer hInet

    hOpen = InternetOpen("App1", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
    sBuffer = Space(1000) && 1000 characters must surely be enough.

    sPath = "http://www.google.co.uk/"
    Ret = 0

    hFile = InternetOpenUrl(hOpen, sPath, 0, 0, INTERNET_FLAG_RELOAD, 0)
    InternetReadFile(hFile, @sBuffer, Len(sBuffer), @Ret)
    InternetCloseHandle(hFile)
    InternetCloseHandle(hOpen)
    STREXTRACT(sBuffer,'<title>','</title>',1,1)


  • MizzouTiger

    This is .net 2005! WebRequest does the trick, thanks. ftpWebRequest seems to relate to transferring files to a server which is not really what I need here.

  • BobK_MN

    Yes you're right .Net documentation sucks. .Net 2005 seems to be better and also there is simplier FtpWebRequest. I also would expect it to find counterparts in .Net when I search for "InternetOpenUrl" or "InternetReadFile".  

  • Mhd. akram

    Oh then I wouldn't be so sure FtpWebRequest is not for download. Check its help. Last time I checked I remember it had an downloadable code sample which shows both download and upload (in C# and VB).


    Update: Check
    http://msdn2.microsoft.com/en-us/library/b7810t5c.aspx

  • Todd West

    Because it's not easy to find out about such classes. The automatic updater doesn't convert your code to use them, if you do a search on  "InternetOpenUrl" in the help documentation you get no results, or if you do a search on "Read Web Page" in the help documentation you get lots of information about Asp.net (even if you have chosen to filter for Visual Basic).

  • Kaz

    Thanks, I have it working now. The difficulties lay in the declaration of the APIs. I created a new VB6 project and used the converter in .net to convert this to .net. A lot of the parameters that I had down as longs were converted to shorts, and things like that in the conversion process. But still I had to spot that a ByRef should have been changed to a ByVal to get it to work. So finally it does.

    This raises a wider question - how do I get hold of API declarations for .net In VB6 there was the handy API viewer utility, but there doesn't seem to be any equivalent in .net.

  • Snkscore

    By the way, here's an example of something I couldn't fix without resorting to use of an API (SetScrollInfo) http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=150781&SiteID=1&PageID=1
  • Lagear

    Yes in your code there were 2 ByVal,ByRef errors.
    Why would you need API like this in .Net It at least is known as .NET and not surprisingly have System.Net namespace. Use classes from there like WebRequest, FtpWebRequest (2.0).

  • Obtaining a web page's title