Usage of the URL Parsing Function parse
Uses parse to parse a URL string and generate a URL object.
Code:
In the example, a URL is parsed to obtain a URL object, and each property of the object is printed.
import encoding.url.*
main(): Int64 {
// Calls the URL static function parse to parse the URL and obtain the object named URL.
var url = URL.parse("http://www.example.com:80/path%E4%BB%93%E9%A2%89?key=value%E4%BB%93%E9%A2%89#%E4%BD%A0%E5%A5%BD")
// Prints the component properties of the URL.
println("url.scheme = ${url.scheme}")
println("url.opaque = ${url.opaque}")
println("url.userInfo = ${url.userInfo}")
println("url.rawUserInfo = ${url.rawUserInfo}")
println("url.hostName = ${url.hostName}")
println("url.port = ${url.port}")
println("url.path = ${url.path}")
println("url.rawPath = ${url.rawPath}")
println("url.query = ${url.query.getOrThrow()}")
println("url.rawQuery = ${url.rawQuery.getOrThrow()}")
println("url.fragment = ${url.fragment.getOrThrow()}")
println("url.rawfragment = ${url.rawFragment.getOrThrow()}")
println("url = ${url}")
return 0
}
Running result:
url.scheme = http
url.opaque =
url.userInfo =
url.rawUserInfo =
url.hostName = www.example.com
url.port = 80
url.path = /path Cangjie
url.rawPath = /path%E4%BB%93%E9%A2%89
url.query = key=value Cangjie
url.rawQuery = key=value%E4%BB%93%E9%A2%89
url.fragment = Hello
url.rawfragment = %E4%BD%A0%E5%A5%BD
url = http://www.example.com:80/path%E4%BB%93%E9%A2%89?key=value%E4%BB%93%E9%A2%89#%E4%BD%A0%E5%A5%BD