Visual Basic Internet Routines
UrlGetPart: Determine the Constituent Parts of a URL
     
Posted:   Monday July 09, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   See prerequisites below
Author:   VBnet - Randy Birch
     

Related:  

UrlUnescape: Encoding and Decoding URL Escape Characters 
UrlCanonicalize: Proper URL Path Encoding and Decoding
UrlCreateFromPath: Proper URL Path Conversion from a DOS Path
     
 Prerequisites
Shlwapi.dll version 5.00 or greater, Windows XP, 2000, Windows NT4 with IE 5 or later, Windows 98, or Windows 95 with IE 5 or later. 

UrlGetPart takes a URL string and returns a portion of the string based on the parameter passed as the dwPart member of the call. The illustration shows the result of a series of calls to UrlGetPart. The Source URL contains pretty well the most you'll find in a single URL - a username and password, the host and port, the path too the data, and additional information as parameters - AKA the query portion.

By calling UrlGetPart once for each part of interest, the separate parts making up the whole are returned. When the URL_PARTFLAG_KEEPSCHEME flag is specified, each part is returned prefaced by the scheme name for the URL.

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1) and a textbox (Text1) for the source URL. Add two additional textboxes in a control array (Text2(0) and Text3(0)) for the return strings from the call, creating the six of each shown. Note that the code used control array indices 1 through 6, so add seven in total and delete the first control added (the 1-6 corresponds to the values passed as dwPart, making calling the API and filling textboxes easier. Once set up, add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const MAX_PATH       As Long = 260
Private Const ERROR_SUCCESS  As Long = 0

Private Const URL_PART_SCHEME As Long = 1
Private Const URL_PART_HOSTNAME As Long = 2
Private Const URL_PART_USERNAME As Long = 3
Private Const URL_PART_PASSWORD As Long = 4
Private Const URL_PART_PORT As Long = 5
Private Const URL_PART_QUERY As Long = 6

Private Const URL_PARTFLAG_KEEPSCHEME As Long = &H1

Private Declare Function UrlGetPart Lib "shlwapi" _
   Alias "UrlGetPartA" _
  (ByVal pszIn As String, _
   ByVal pszOut As String, _
   pcchOut As Long, _
   ByVal dwPart As Long, _
   ByVal dwFlags As Long) As Long


Private Sub Form_Load()

   Dim cnt As Long
   
   Text1.Text = "http://randy:password" & _
                "@www.mvps.org:8080" & _
                "/vbnet/index.html?" & _
                "code/network/netconnect.htm"
     
   For cnt = 1 To 6
      Text2(cnt).Text = ""
      Text3(cnt).Text = ""
   Next
      
   Command1.Caption = "Get Parts"

End Sub


Private Sub Command1_Click()

   Dim sPart As String
   Dim sUrl As String
   Dim cnt As Long
   
  'using the original string in Text1 for
  'all calls, show the results of calling
  'UrlGetPart passing 0& and Keepscheme as
  'flags.
  
   For cnt = 1 To 6
      sUrl = Text1.Text
      sPart = GetUrlParts(sUrl, cnt, 0&)
      Text2(cnt).Text = sPart
   Next
   
   For cnt = 1 To 6
      sUrl = Text1.Text
      sPart = GetUrlParts(sUrl, cnt, URL_PARTFLAG_KEEPSCHEME)
      Text3(cnt).Text = sPart
   Next
   
End Sub


Private Function GetUrlParts(ByVal sUrl As String, _
                             dwPart As Long, _
                             dwFlags As Long) As String

   Dim sPart As String
   Dim dwSize As Long
   
   If Len(sUrl) > 0 Then
   
      sPart = Space$(MAX_PATH)
      dwSize = Len(sPart)
     
      If UrlGetPart(sUrl, _
                    sPart, _
                    dwSize, _
                    dwPart, _
                    dwFlags) = ERROR_SUCCESS Then
   
         GetUrlParts = Left$(sPart, dwSize)
         
      End If  'If UrlGetPart
   End If 'If Len(sUrl) > 0

End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter