|
-
May 13th, 2007, 07:55 PM
#1
Thread Starter
New Member
(VB6)Accessing the contents of a file on a remote computer
How can I get my program to open a file on a remote server?
For example, if I wanted to open the following file and read the contents into a string: http://lueshi.fortunecity.com/password.txt
What do I need to do? I'm completely new to network operations with VB, so go easy on me.
-
May 14th, 2007, 01:18 AM
#2
Re: (VB6)Accessing the contents of a file on a remote computer
 Originally Posted by e946
How can I get my program to open a file on a remote server?
For example, if I wanted to open the following file and read the contents into a string: http://lueshi.fortunecity.com/password.txt
What do I need to do? I'm completely new to network operations with VB, so go easy on me.
Copy/paste this into a module (I found this module on the net so don't give me credit ):
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(URL 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, URL, 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
Then, in your project you can use that function like:
vb Code:
Private Sub Command1_Click()
Dim strHTML As String
strHTML = GetUrlSource("http://lueshi.fortunecity.com/password.txt")
MsgBox strHTML
End Sub
-
May 14th, 2007, 01:58 AM
#3
Thread Starter
New Member
Re: (VB6)Accessing the contents of a file on a remote computer
That worked and I connected to the internet and got the file, but as I stepped through it, it showed a lot of garbage characters on the end. It should be easy for me to get rid of that now, but later on I'll be encrypting this and purposefully throwing garbage in there so people can't pick out the actual password, so how do I differentiate the purposeful garbage from the unwanted garbage? See image for an example
Last edited by e946; May 14th, 2007 at 02:02 AM.
-
May 14th, 2007, 02:19 AM
#4
Thread Starter
New Member
Re: (VB6)Accessing the contents of a file on a remote computer
Another question: How can I prevent the user from implementing a hosts file to redirect the request to the website of their choice? (My intention is to use this web server to change the password every week or so, but if they use a hosts file and save my password file elsewhere, they can just use the same password indefinitely). I had thought of working the URL information into the encryption, but it doesn't look like I'll be able to compare it with anything, unless there's a function that will return the URL of hInternet.
-
May 14th, 2007, 02:36 AM
#5
Re: (VB6)Accessing the contents of a file on a remote computer
 Originally Posted by e946
That worked and I connected to the internet and got the file, but as I stepped through it, it showed a lot of garbage characters on the end. It should be easy for me to get rid of that now, but later on I'll be encrypting this and purposefully throwing garbage in there so people can't pick out the actual password, so how do I differentiate the purposeful garbage from the unwanted garbage? See image for an example

I noticed that too (although not nearly as bad as your screenshot shows ). For your situation, you might want to consider using the Inet control.
In VB, press CTRL+T to open the components dialog. Find "Microsoft Internet Transfer Control" and add it to your form. Then you can use:
strHTML = Inet1.Open("http://lueshi.fortunecity.com/password.txt")
 Originally Posted by e946
Another question: How can I prevent the user from implementing a hosts file to redirect the request to the website of their choice? (My intention is to use this web server to change the password every week or so, but if they use a hosts file and save my password file elsewhere, they can just use the same password indefinitely). I had thought of working the URL information into the encryption, but it doesn't look like I'll be able to compare it with anything, unless there's a function that will return the URL of hInternet.
Not sure I get what you're asking. Maybe an example?
-
May 14th, 2007, 11:03 AM
#6
Thread Starter
New Member
Re: (VB6)Accessing the contents of a file on a remote computer
c:\windows\system32\drivers\etc\hosts
^If you're running windows, paste that into your browser. Windows looks to that file before it looks to a DNS server. So if you wanted vbforums.com to redirect to a computer on your local network, you could make an entry like
192.168.0.4 vbforums.com
This could be used to override my (admittedly weak) security. If a user was good enough with computers, he/she could make an entry that would send all requests to "lueshi.fortunecity.com" to an alternative web address of their choosing.
Also, VB crashes when I try to add Microsift Internet Controls
-
May 14th, 2007, 11:19 AM
#7
Re: (VB6)Accessing the contents of a file on a remote computer
 Originally Posted by e946
c:\windows\system32\drivers\etc\hosts
^If you're running windows, paste that into your browser. Windows looks to that file before it looks to a DNS server. So if you wanted vbforums.com to redirect to a computer on your local network, you could make an entry like
192.168.0.4 vbforums.com
This could be used to override my (admittedly weak) security. If a user was good enough with computers, he/she could make an entry that would send all requests to "lueshi.fortunecity.com" to an alternative web address of their choosing.
Also, VB crashes when I try to add Microsift Internet Controls
It shouldn't crash. But make sure you're adding "Microsoft Internet Transfer Control". The other one is the web browser control.
Never thought of the hosts file as a security risk (thanks for that because that's something I need to consider for a project I'm working on).
Perhaps you can check the hosts file for an entry that has your domain in it. If it's there, then you can probably remove it programatically or alert the user that they need to change it.
Not really sure of any other way around that.
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
|