Results 1 to 14 of 14

Thread: Downloading URL code

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Downloading URL code

    Hi,
    In the past I used some API to download source code of a website cause it's the FASTER way I found.

    These was the code in VB6.0:

    VB Code:
    1. Option Explicit
    2. Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    3. Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    4. Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    5. Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    6.  
    7. Public Const IF_FROM_CACHE = &H1000000
    8. Public Const IF_MAKE_PERSISTENT = &H2000000
    9. Public Const IF_NO_CACHE_WRITE = &H4000000
    10.        
    11. Private Const BUFFER_LEN = 256
    12.  
    13.  
    14. Public Function GetUrlSource(sURL As String) As String
    15.     Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    16.     Dim hInternet As Long, hSession As Long, lReturn As Long
    17.  
    18.     'get the handle of the current internet connection
    19.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    20.     'get the handle of the url
    21.     If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    22.     'if we have the handle, then start reading the web page
    23.     If hInternet Then
    24.         'get the first chunk & buffer it.
    25.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    26.         sData = sBuffer
    27.         'if there's more data then keep reading it into the buffer
    28.         Do While lReturn <> 0
    29.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    30.             sData = sData + Mid(sBuffer, 1, lReturn)
    31.         Loop
    32.     End If
    33.    
    34.     'close the URL
    35.     iResult = InternetCloseHandle(hInternet)
    36.  
    37.     GetUrlSource = sData
    38. End Function
    39.  
    40. Now in vb.net I can't do that or I haven't found a way to do the same code...
    41.  
    42. I have few question:
    43.  
    44. 1- Can we use vb6.0 api in vb.net?
    45. 2- Is there a faster way to download source code?
    46. 3- How can I changed the code below to vb.net?

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    to download source you could try using the .Net.Webclient, eg:
    VB Code:
    1. [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
    2.         [COLOR=BLUE]Dim[/COLOR] client [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] Net.WebClient()
    3.         [COLOR=BLUE]Dim[/COLOR] sReader [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(client.OpenRead("http://www.vbforums.com/showthread.php?s=&threadid=259119"))
    4.         MessageBox.Show(sReader.ReadToEnd)
    5.         client.Dispose()
    6.         sReader.Close()
    7.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]

    to use your api's , replace all the Longs with Integers.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Thx the code work perfectly

  4. #4

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    What if I need to be connected before coming to the page? How can I do that... I need to log and press a button than download the source page...

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    and I can't use the API cause of the Buffer variable

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i wouldnt even bother with the api version, here's 2 more replacements you could try ( which basically are updated versions of the api )
    **** make sure you put this .... Imports System.Net '/// at the top of your form's code window.
    then...
    VB Code:
    1. [COLOR=GREEN]'///Way 1 .....
    2. [/COLOR]    [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
    3.         [COLOR=BLUE]Dim[/COLOR] Request [COLOR=BLUE]As[/COLOR] WebRequest = WebRequest.Create("http://google.com")
    4.         [COLOR=BLUE]Dim[/COLOR] bt [COLOR=BLUE]As[/COLOR] WebResponse = Request.GetResponse
    5.         [COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(bt.GetResponseStream)
    6.         MessageBox.Show(sr.ReadToEnd)
    7.         sr.Close()
    8.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub
    9.  
    10. [/COLOR]    [COLOR=GREEN]'/////
    11. [/COLOR]    [COLOR=GREEN]'////Way 2.....
    12. [/COLOR]    [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button2_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button2.Click
    13.         [COLOR=BLUE]Dim[/COLOR] httpReq [COLOR=BLUE]As[/COLOR] HttpWebRequest = [COLOR=BLUE]DirectCast[/COLOR](WebRequest.Create("http://google.com"), HttpWebRequest)
    14.         [COLOR=BLUE]Dim[/COLOR] httpResp [COLOR=BLUE]As[/COLOR] HttpWebResponse = httpReq.GetResponse
    15.         [COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(httpResp.GetResponseStream)
    16.         MessageBox.Show(sr.ReadToEnd)
    17.         sr.Close()
    18.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    What i have done is that i connect VIA the webbrowser control. I log on then press conect then I have to download few page but very fast but with all your code i can't download cause it say that I am not connected... the API took in consideration the login that I have made in the the webbrowser object (well in Vb6.0) but now I am completly lost with vb.net...

    Any way to connect via code? I really do not want to use the webbrowser object or only for connection...

  8. #8

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Here is what I changed from the API code that worked in vb6.0 but it return always Nothing.

    VB Code:
    1. Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
    2.     Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Integer, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
    3.     Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Integer, ByVal sBuffer As String, ByVal lNumBytesToRead As Integer, ByRef lNumberOfBytesRead As Integer) As Integer
    4.     Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByRef hInet As Integer) As Integer
    5.  
    6.     Public Const IF_FROM_CACHE = &H1000000
    7.     Public Const IF_MAKE_PERSISTENT = &H2000000
    8.     Public Const IF_NO_CACHE_WRITE = &H4000000
    9.  
    10.     Private Const BUFFER_LEN = 256
    11.  
    12.     Public Function GetUrlSource(ByVal sURL As String) As String
    13.         Dim sBuffer As String, iResult As Integer, sData As String
    14.         Dim hInternet As Integer, hSession As Integer, lReturn As Integer
    15.  
    16.         'get the handle of the current internet connection
    17.         hSession = InternetOpen("Microsoft Internet Explorer", 1, Nothing, Nothing, 0)
    18.         'get the handle of the url
    19.         If hSession Then
    20.             hInternet = InternetOpenUrl(hSession, sURL, Nothing, 0, IF_NO_CACHE_WRITE, 0)
    21.         End If
    22.         'if we have the handle, then start reading the web page
    23.         If hInternet Then
    24.             'get the first chunk & buffer it.
    25.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    26.             sData = sBuffer
    27.             'if there's more data then keep reading it into the buffer
    28.             Do While lReturn <> 0
    29.                 iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    30.                 sData = sData + Mid(sBuffer, 1, lReturn)
    31.             Loop
    32.         End If
    33.  
    34.         'close the URL
    35.         iResult = InternetCloseHandle(hInternet)
    36.  
    37.         GetUrlSource = sData
    38.     End Function

    What do I convert bad?

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    the full api version wont work, but you can open the internet connection, then get the source like this....
    VB Code:
    1. [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Declare[/COLOR] [COLOR=BLUE]Function[/COLOR] InternetOpen [COLOR=BLUE]Lib[/COLOR] "wininet" [COLOR=BLUE]Alias[/COLOR] "InternetOpenA" ([COLOR=BLUE]ByVal[/COLOR] sAgent [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR], [COLOR=BLUE]ByVal[/COLOR] lAccessType [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Long[/COLOR], [COLOR=BLUE]ByVal[/COLOR] sProxyName [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR], [COLOR=BLUE]ByVal[/COLOR] sProxyBypass [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR], [COLOR=BLUE]ByVal[/COLOR] lFlags [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer[/COLOR]) [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
    2. [/COLOR]    [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Declare[/COLOR] [COLOR=BLUE]Function[/COLOR] InternetCloseHandle [COLOR=BLUE]Lib[/COLOR] "wininet" ([COLOR=BLUE]ByRef[/COLOR] hInet [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer[/COLOR]) [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
    3. [/COLOR]    [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Const[/COLOR] INTERNET_OPEN_TYPE_DIRECT [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer[/COLOR] = 1
    4.  
    5.     [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
    6.         [COLOR=BLUE]Dim[/COLOR] sAgent [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = "some agent"
    7.         [COLOR=BLUE]Dim[/COLOR] Open [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer[/COLOR] = InternetOpen(sAgent, INTERNET_OPEN_TYPE_DIRECT, [COLOR=BLUE]Nothing[/COLOR], [COLOR=BLUE]Nothing[/COLOR], [COLOR=BLUE]Nothing[/COLOR])
    8.         [COLOR=GREEN]'/// open the internet connection ^^^
    9. [/COLOR]        [COLOR=BLUE]Dim[/COLOR] Request [COLOR=BLUE]As[/COLOR] WebRequest = WebRequest.Create("http://google.com")
    10.         [COLOR=BLUE]Dim[/COLOR] bt [COLOR=BLUE]As[/COLOR] WebResponse = Request.GetResponse
    11.         [COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(bt.GetResponseStream)
    12.         MessageBox.Show(sr.ReadToEnd)
    13.         sr.Close()
    14.         [COLOR=GREEN]'/// then close the internet connection...
    15. [/COLOR]        InternetCloseHandle(Open)
    16.  
    17.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
    hope that helps.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Doesn't work I still can have the code but I can't have the right page, i have the page that told me that I am not connected when I connected in the webbrowser :\

  11. #11

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Why the full API version won't work?

    Do you think I can do an OCX in vb6 and use it in .Net?

  12. #12
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you could do yeah, although you should be able to do it through .net really.
    but if you create an activeX ocx / dll in vb6, then make a reference to it in .net and call a function from within the ocx, you may just get it to work.
    cant really be much more help from my end because i'm running broadband which is permanently connected
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  13. #13

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I am always connected my problem is that when I connect VIA a website in php and I need to login to go to the other pages well I can't get it if I do not log cause a session start. That why I use the webbrowser control in my project and than the API cause with the webbrowser I connect manually than I click a button and all desirer page are downloaded with the API. I dont know why with the API it take the same session of the webbrowser... all your .net code don't :\ (but thx for the help)

  14. #14

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I have made a DLL with some modification of the API that I posted before and it worked in vb.net... weird that I need to use vb6.0 in vb.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width