INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

open URL and store response

open URL and store response

(OP)
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

RE: open URL and store response

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


RE: open URL and store response

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 <http://www.tek-tips.com/viewthread.cfm?qid=968818&amp;page=3>

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

RE: open URL and store response

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

RE: open URL and store response

How would a user name and password be passed into ZeBula8's code?
Thanks!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close