Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing info from URL call 1

Status
Not open for further replies.

kylua

Technical User
Joined
Sep 30, 2002
Messages
199
Location
GB
Hi all, and especially Zebula8 who wrote the below:

CODE

Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public Declare Function InternetOpen Lib "wininet" 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 InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Public Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sbuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long



Private Function GetFile(ByVal sURL As String) As Integer
' Reads the data from a web page to a buffer and downloads it to a user's machine
'
'
Dim hOpen As Long
Dim hFile As Long
Dim sbuffer As String
Dim Ret As Long
'Dim lFilenumber As Long
Dim oFS As Object
Dim oTxtStream As Object

On Error GoTo GetFile_Error

GetFile = 0

' create a buffer for the file we're going to download
' you'll have to decide ahead of time how large the file that is to be downloaded is and adjust accordingly
sbuffer = Space(2000)

' create an internet connection
hOpen = InternetOpen("userAgent", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)

' open the url
hFile = InternetOpenUrl(hOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)


If hFile <> 0 Then
' read the bytes of the file
InternetReadFile hFile, sbuffer, 2000, Ret


' clean up
InternetCloseHandle hFile
InternetCloseHandle hOpen

' create file system object
Set oFS = CreateObject("Scripting.FileSystemObject")

' create a textstream - parameter of true means it will be unicode
Set oTxtStream = oFS.CreateTextFile("c:\PathToFile\myFile.txt", True)

' now we have our textstream we can write to it and then close it
oTxtStream.Write Trim$(sbuffer)
oTxtStream.Close

Set oTxtStream = Nothing
Set oFS = Nothing
GetFile = 1
End If

On Error GoTo 0
Exit Function

GetFile_Error:

MsgBox "Error " & Err.Number ' put your own handling here
End Function

Many thanks for this code, I have used it to build a 'Web Task Scheduler' that calls .asp scripts to perform a function and writes the results to screen and details to a text log. It's running about six scripts varying from daily to every minute and it works great when it's running from my home pc.
But I uploaded it my server and installed it and it works fine for any URLs that are elsewhere on the net but doesn't return anything from any script stored on the server itself.
It doesn't use URL look up but the direct IP address, ie 85.215.12.32, (there is no actual URL for the server), but this made no difference when it was running off a different machine.
But it's OK to call a script on another URL that then does a GET request of the server script, but not a redirect.

Any hints at how to get round this?
 
Have you tried using 127.0.0.1 for these scripts?
 
Brilliant bit of lateral thinking there young sir/madam!!

Works a treat, (altho I have to admit I didn't have a clue what you were talking about at first!)

Have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top