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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open URL and store response 1

Status
Not open for further replies.

capitano

Programmer
Jul 30, 2001
88
US
Hi,

I'm just learning VB.NET. Can somebody tell me how to open a http URL from within a VB application? I want to open the URL, retrieve an XML document from the web, store the XML into a local variable, and manipulate the XML -- all of this without the user having to see the website or the XML. I want it to happen "under the hood", inside the application.

Thanks for any help.
Bryan
 
i wrote this in vb6 but you can easily port it to .net for your needs - it's pretty straightforward code:

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
 
ZeBula8,

Hey, I have a question for you regarding this snippet...

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

My question focuses more on the "user agent" part. Are you defining who, or what, the user agent is here? For instance, is the UserAgent "Internet_Open_type_direct", or is it VBNUllstring, or neither?

Please see my question titled "Renaming the Microsoft URL Control in WebLogs", in Forum 222 "Visual Basic(Microsoft): 5 & 6", which is currently here <
If I have to use the API in order for this to happen, then I am all for it. I want to do something similar to what Capitano wants to do where I search a webpage for various things. Thanks for any input you can provide.

LF
 
ZeBula8,

You are the BOMB! Thank you so much. Needless to say, I figured it out!

I gave you one star, and I tried to give you another, but I couldn't, sorry. I really appreciate this code though. You really went above and beyond the call of duty. Thank you! Thank you! Thank You! I can't thank you enough!

LF
 
How would a user name and password be passed into ZeBula8's code?
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top