Table of Contents

24.6.7 Class URL

The class URL (gb.web) provides four (static) methods for handling URL strings. A URL (Uniform Resource Locator) is an identifier for a network resource.

24.6.7.1 Methods

The URL class has only these four methods:

MethodReturnTypeDescription
Encode ( Path As String )String Encodes a URL into the Application/x-www-form-urlencoded format.
Decode ( Path As String )String Decodes a URL from the Application/x-www-form-urlencoded-Format.
SetQuery ( URL As String, Field As String, Value As String )String Sets the value of the specified field in a query and returns the modified query. If the specified field does not exist, it will be added to the query.
UnsetQuery ( URL As String, Field As String )String Removes the specified field from the query URL and returns the modified query. If the URL has no field parameter, then the unmodified query is returned.

Table 24.6.7.1.1 : Methods of the class URL

The two methods URL.Encode and URL.Decode from the class URL listed in Table 24.6.7.1.1 correspond to the methods of the class CGI (gb.web) with the same name, which are described in Chapter 24.6.1 CGI.

24.6.7.2 Examples

The following examples use the four methods of the URL class.The specified URLs are first encoded into the application/x-www-form-urlencoded format and then de-encoded:

Print URL.Encode("http://www.gambas-buch.de/dw/doku.php?id=k18:start")
Print URL.Decode("http%3A%2F%2Fwww.gambas-buch.de%2Fdw%2Fdoku.php%3Fid%3Dk18%3Astart")
http%3A%2F%2Fwww.gambas-buch.de%2Fdw%2Fdoku.php%3Fid%3Dk18%3Astart
http://www.gambas-buch.de/dw/doku.php?id=k18:start

These examples demonstrate the modification of an existing HTTP request:

Print SetQuery("http://example.com/test", "a", "1") 		' Das Feld `a` bekommt den Wert 1
http://example.com/test?a=1

Print SetQuery("http://example.com/test?a=1", "b", "2")    	' Das Feld `b` wird hinzugefügt mit dem Wert 2
http://example.com/test?a=1&b=2

Print SetQuery("http://example.com/test?a=1&b=2", "a", "3")	' Nur das Feld `a` bekommt den Wert 3
http://example.com/test?b=2&a=3

Print SetQuery("http://example.com/test?b=2&a=3", "b", "")	' Nur das Feld `b` bekommt einen Leerstring
http://example.com/test?a=3&b

The next calls to the UnsetQuery(…) method will remove fields from a request. But only if the specified field exists in the original of the request:

Print UnsetQuery("http://example.com/test?a=3&b", "a") ' Das Feld `a` wird aus der Anfrage entfernt
http://example.com/test?b

Print UnsetQuery("http://example.com/test?b", "a")     ' Das Feld `a` wird aus der Anfrage entfernt
http://example.com/test?b

Print UnsetQuery("http://example.com/test?b", "b")     ' Das Feld `b` wird aus der Anfrage entfernt
http://example.com/test

Print UnsetQuery("http://example.com/test?a=3&b")      ' Die Anfrage wird im Original zurückgegeben
http://example.com/test? a=3&b