|
-
Aug 27th, 2003, 10:46 AM
#1
Thread Starter
Ya ya Baby!!!Me is Back
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:
Option Explicit
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
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
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Const IF_FROM_CACHE = &H1000000
Public Const IF_MAKE_PERSISTENT = &H2000000
Public Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256
Public Function GetUrlSource(sURL As String) As String
Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
Dim hInternet As Long, hSession As Long, lReturn As Long
'get the handle of the current internet connection
hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
'get the handle of the url
If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
'if we have the handle, then start reading the web page
If hInternet Then
'get the first chunk & buffer it.
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sBuffer
'if there's more data then keep reading it into the buffer
Do While lReturn <> 0
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sData + Mid(sBuffer, 1, lReturn)
Loop
End If
'close the URL
iResult = InternetCloseHandle(hInternet)
GetUrlSource = sData
End Function
Now in vb.net I can't do that or I haven't found a way to do the same code...
I have few question:
1- Can we use vb6.0 api in vb.net?
2- Is there a faster way to download source code?
3- How can I changed the code below to vb.net?
-
Aug 27th, 2003, 11:02 AM
#2
to download source you could try using the .Net.Webclient, eg:
VB Code:
[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
[COLOR=BLUE]Dim[/COLOR] client [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] Net.WebClient()
[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"))
MessageBox.Show(sReader.ReadToEnd)
client.Dispose()
sReader.Close()
[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]
-
Aug 27th, 2003, 02:49 PM
#3
Thread Starter
Ya ya Baby!!!Me is Back
Thx the code work perfectly
-
Aug 27th, 2003, 05:06 PM
#4
Thread Starter
Ya ya Baby!!!Me is Back
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...
-
Aug 27th, 2003, 05:15 PM
#5
Thread Starter
Ya ya Baby!!!Me is Back
and I can't use the API cause of the Buffer variable
-
Aug 27th, 2003, 06:47 PM
#6
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:
[COLOR=GREEN]'///Way 1 .....
[/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
[COLOR=BLUE]Dim[/COLOR] Request [COLOR=BLUE]As[/COLOR] WebRequest = WebRequest.Create("http://google.com")
[COLOR=BLUE]Dim[/COLOR] bt [COLOR=BLUE]As[/COLOR] WebResponse = Request.GetResponse
[COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(bt.GetResponseStream)
MessageBox.Show(sr.ReadToEnd)
sr.Close()
[COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub
[/COLOR] [COLOR=GREEN]'/////
[/COLOR] [COLOR=GREEN]'////Way 2.....
[/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
[COLOR=BLUE]Dim[/COLOR] httpReq [COLOR=BLUE]As[/COLOR] HttpWebRequest = [COLOR=BLUE]DirectCast[/COLOR](WebRequest.Create("http://google.com"), HttpWebRequest)
[COLOR=BLUE]Dim[/COLOR] httpResp [COLOR=BLUE]As[/COLOR] HttpWebResponse = httpReq.GetResponse
[COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(httpResp.GetResponseStream)
MessageBox.Show(sr.ReadToEnd)
sr.Close()
[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]
-
Aug 28th, 2003, 01:02 PM
#7
Thread Starter
Ya ya Baby!!!Me is Back
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...
-
Aug 28th, 2003, 02:43 PM
#8
Thread Starter
Ya ya Baby!!!Me is Back
Here is what I changed from the API code that worked in vb6.0 but it return always Nothing.
VB Code:
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
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
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
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByRef hInet As Integer) As Integer
Public Const IF_FROM_CACHE = &H1000000
Public Const IF_MAKE_PERSISTENT = &H2000000
Public Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256
Public Function GetUrlSource(ByVal sURL As String) As String
Dim sBuffer As String, iResult As Integer, sData As String
Dim hInternet As Integer, hSession As Integer, lReturn As Integer
'get the handle of the current internet connection
hSession = InternetOpen("Microsoft Internet Explorer", 1, Nothing, Nothing, 0)
'get the handle of the url
If hSession Then
hInternet = InternetOpenUrl(hSession, sURL, Nothing, 0, IF_NO_CACHE_WRITE, 0)
End If
'if we have the handle, then start reading the web page
If hInternet Then
'get the first chunk & buffer it.
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sBuffer
'if there's more data then keep reading it into the buffer
Do While lReturn <> 0
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sData + Mid(sBuffer, 1, lReturn)
Loop
End If
'close the URL
iResult = InternetCloseHandle(hInternet)
GetUrlSource = sData
End Function
What do I convert bad?
-
Aug 28th, 2003, 07:14 PM
#9
the full api version wont work, but you can open the internet connection, then get the source like this....
VB Code:
[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
[/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
[/COLOR] [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Const[/COLOR] INTERNET_OPEN_TYPE_DIRECT [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer[/COLOR] = 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
[COLOR=BLUE]Dim[/COLOR] sAgent [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = "some agent"
[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])
[COLOR=GREEN]'/// open the internet connection ^^^
[/COLOR] [COLOR=BLUE]Dim[/COLOR] Request [COLOR=BLUE]As[/COLOR] WebRequest = WebRequest.Create("http://google.com")
[COLOR=BLUE]Dim[/COLOR] bt [COLOR=BLUE]As[/COLOR] WebResponse = Request.GetResponse
[COLOR=BLUE]Dim[/COLOR] sr [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader(bt.GetResponseStream)
MessageBox.Show(sr.ReadToEnd)
sr.Close()
[COLOR=GREEN]'/// then close the internet connection...
[/COLOR] InternetCloseHandle(Open)
[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]
-
Aug 29th, 2003, 06:16 PM
#10
Thread Starter
Ya ya Baby!!!Me is Back
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 :\
-
Aug 29th, 2003, 06:20 PM
#11
Thread Starter
Ya ya Baby!!!Me is Back
Why the full API version won't work?
Do you think I can do an OCX in vb6 and use it in .Net?
-
Aug 29th, 2003, 06:24 PM
#12
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]
-
Aug 29th, 2003, 06:57 PM
#13
Thread Starter
Ya ya Baby!!!Me is Back
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 )
-
Aug 29th, 2003, 09:54 PM
#14
Thread Starter
Ya ya Baby!!!Me is Back
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|