PowerShell
Codifica / decodifica URL
Ricerca…
Osservazioni
L'espressione regolare utilizzata negli esempi di URL di decodifica è stata presa da RFC 2396, Appendice B: analisi di un riferimento URI con un'espressione regolare ; per i posteri, ecco una citazione:
La seguente riga è l'espressione regolare per suddividere un riferimento URI nei suoi componenti.
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 12 3 4 5 6 7 8 9
I numeri nella seconda riga in alto servono solo per facilitare la leggibilità; indicano i punti di riferimento per ogni sottoespressione (cioè, ogni parentesi accoppiata). Ci riferiamo al valore abbinato per subexpression come $. Ad esempio, facendo corrispondere l'espressione sopra a
http://www.ics.uci.edu/pub/ietf/uri/#Related
restituisce le seguenti corrispondenze di sottoespressione:
$1 = http: $2 = http $3 = //www.ics.uci.edu $4 = www.ics.uci.edu $5 = /pub/ietf/uri/ $6 = <undefined> $7 = <undefined> $8 = #Related $9 = Related
Avvio rapido: codifica
$url1 = [uri]::EscapeDataString("http://test.com?test=my value")
# url1: http%3A%2F%2Ftest.com%3Ftest%3Dmy%20value
$url2 = [uri]::EscapeUriString("http://test.com?test=my value")
# url2: http://test.com?test=my%20value
# HttpUtility requires at least .NET 1.1 to be installed.
$url3 = [System.Web.HttpUtility]::UrlEncode("http://test.com?test=my value")
# url3: http%3a%2f%2ftest.com%3ftest%3dmy+value
Nota: ulteriori informazioni su HTTPUtility .
Avvio rapido: decodifica
Nota: questi esempi utilizzano le variabili create nella sezione Avvio rapido: Codifica sopra.
# url1: http%3A%2F%2Ftest.com%3Ftest%3Dmy%20value
[uri]::UnescapeDataString($url1)
# Returns: http://test.com?test=my value
# url2: http://test.com?test=my%20value
[uri]::UnescapeDataString($url2)
# Returns: http://test.com?test=my value
# url3: http%3a%2f%2ftest.com%3ftest%3dmy+value
[uri]::UnescapeDataString($url3)
# Returns: http://test.com?test=my+value
# Note: There is no `[uri]::UnescapeUriString()`;
# which makes sense since the `[uri]::UnescapeDataString()`
# function handles everything it would handle plus more.
# HttpUtility requires at least .NET 1.1 to be installed.
# url1: http%3A%2F%2Ftest.com%3Ftest%3Dmy%20value
[System.Web.HttpUtility]::UrlDecode($url1)
# Returns: http://test.com?test=my value
# HttpUtility requires at least .NET 1.1 to be installed.
# url2: http://test.com?test=my%20value
[System.Web.HttpUtility]::UrlDecode($url2)
# Returns: http://test.com?test=my value
# HttpUtility requires at least .NET 1.1 to be installed.
# url3: http%3a%2f%2ftest.com%3ftest%3dmy+value
[System.Web.HttpUtility]::UrlDecode($url3)
# Returns: http://test.com?test=my value
Nota: ulteriori informazioni su HTTPUtility .
Codifica stringa di query con `[uri] :: EscapeDataString ()`
$scheme = 'https'
$url_format = '{0}://example.vertigion.com/foos?{1}'
$qs_data = @{
'foo1'='bar1';
'foo2'= 'complex;/?:@&=+$, bar''"';
'complex;/?:@&=+$, foo''"'='bar2';
}
[System.Collections.ArrayList] $qs_array = @()
foreach ($qs in $qs_data.GetEnumerator()) {
$qs_key = [uri]::EscapeDataString($qs.Name)
$qs_value = [uri]::EscapeDataString($qs.Value)
$qs_array.Add("${qs_key}=${qs_value}") | Out-Null
}
$url = $url_format -f @([uri]::"UriScheme${scheme}", ($qs_array -join '&'))
Con [uri]::EscapeDataString()
, noterai che l'apostrofo ( '
) non è stato codificato:
https://example.vertigion.com/foos ? foo2 = complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20bar '% 22 e complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20foo '% 22 = bar2 & foo1 = bar1
Codifica stringa di query con `[System.Web.HttpUtility] :: UrlEncode ()`
$scheme = 'https'
$url_format = '{0}://example.vertigion.com/foos?{1}'
$qs_data = @{
'foo1'='bar1';
'foo2'= 'complex;/?:@&=+$, bar''"';
'complex;/?:@&=+$, foo''"'='bar2';
}
[System.Collections.ArrayList] $qs_array = @()
foreach ($qs in $qs_data.GetEnumerator()) {
$qs_key = [System.Web.HttpUtility]::UrlEncode($qs.Name)
$qs_value = [System.Web.HttpUtility]::UrlEncode($qs.Value)
$qs_array.Add("${qs_key}=${qs_value}") | Out-Null
}
$url = $url_format -f @([uri]::"UriScheme${scheme}", ($qs_array -join '&'))
Con [System.Web.HttpUtility]::UrlEncode()
, noterai che gli spazi diventano più segni ( +
) invece di %20
:
https://example.vertigion.com/foos ? foo2 = complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 e complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + foo% 27% 22 = bar2 & foo1 = bar1
Decodifica URL con `[uri] :: UnescapeDataString ()`
Codificato con [uri]::EscapeDataString()
Innanzitutto, decodificare l'URL e la stringa di query codificati con [uri]::EscapeDataString()
nell'esempio precedente:
https://example.vertigion.com/foos ? foo2 = complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20bar '% 22 e complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20foo '% 22 = bar2 & foo1 = bar1
$url = 'https://example.vertigion.com/foos?foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar''%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo''%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
if ($url -match $url_parts_regex) {
$url_parts = @{
'Scheme' = $Matches[2];
'Server' = $Matches[4];
'Path' = $Matches[5];
'QueryString' = $Matches[7];
'QueryStringParts' = @{}
}
foreach ($qs in $query_string.Split('&')) {
$qs_key, $qs_value = $qs.Split('=')
$url_parts.QueryStringParts.Add(
[uri]::UnescapeDataString($qs_key),
[uri]::UnescapeDataString($qs_value)
) | Out-Null
}
} else {
Throw [System.Management.Automation.ParameterBindingException] "Invalid URL Supplied"
}
Questo ti restituisce [hashtable]$url_parts
; che è uguale a ( Nota: gli spazi nelle parti complesse sono spazi ):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar'%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo'%22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Codificato con [System.Web.HttpUtility]::UrlEncode()
Ora, decodificheremo l'URL e la stringa di query codificata con [System.Web.HttpUtility]::UrlEncode()
nell'esempio precedente:
https://example.vertigion.com/foos ? foo2 = complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 e complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + foo% 27% 22 = bar2 & foo1 = bar1
$url = 'https://example.vertigion.com/foos?foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
if ($url -match $url_parts_regex) {
$url_parts = @{
'Scheme' = $Matches[2];
'Server' = $Matches[4];
'Path' = $Matches[5];
'QueryString' = $Matches[7];
'QueryStringParts' = @{}
}
foreach ($qs in $query_string.Split('&')) {
$qs_key, $qs_value = $qs.Split('=')
$url_parts.QueryStringParts.Add(
[uri]::UnescapeDataString($qs_key),
[uri]::UnescapeDataString($qs_value)
) | Out-Null
}
} else {
Throw [System.Management.Automation.ParameterBindingException] "Invalid URL Supplied"
}
Questo ti restituisce [hashtable]$url_parts
, che equivale a ( Nota: gli spazi nelle parti complesse sono più segni ( +
) nella prima parte e spazi nella seconda parte):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Decodifica l'URL con `[System.Web.HttpUtility] :: UrlDecode ()`
Codificato con [uri]::EscapeDataString()
Innanzitutto, decodificare l'URL e la stringa di query codificati con [uri]::EscapeDataString()
nell'esempio precedente:
https://example.vertigion.com/foos ? foo2 = complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20bar '% 22 e complesso% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 20foo '% 22 = bar2 & foo1 = bar1
$url = 'https://example.vertigion.com/foos?foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar''%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo''%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
if ($url -match $url_parts_regex) {
$url_parts = @{
'Scheme' = $Matches[2];
'Server' = $Matches[4];
'Path' = $Matches[5];
'QueryString' = $Matches[7];
'QueryStringParts' = @{}
}
foreach ($qs in $query_string.Split('&')) {
$qs_key, $qs_value = $qs.Split('=')
$url_parts.QueryStringParts.Add(
[System.Web.HttpUtility]::UrlDecode($qs_key),
[System.Web.HttpUtility]::UrlDecode($qs_value)
) | Out-Null
}
} else {
Throw [System.Management.Automation.ParameterBindingException] "Invalid URL Supplied"
}
Questo ti restituisce [hashtable]$url_parts
; che è uguale a ( Nota: gli spazi nelle parti complesse sono spazi ):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString foo2=complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20bar'%22&complex%3B%2F%3F%3A%40%26%3D%2B%24%2C%20foo'%22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1
Codificato con [System.Web.HttpUtility]::UrlEncode()
Ora, decodificheremo l'URL e la stringa di query codificata con [System.Web.HttpUtility]::UrlEncode()
nell'esempio precedente:
https://example.vertigion.com/foos ? foo2 = complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 e complesso% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + foo% 27% 22 = bar2 & foo1 = bar1
$url = 'https://example.vertigion.com/foos?foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1'
$url_parts_regex = '^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' # See Remarks
if ($url -match $url_parts_regex) {
$url_parts = @{
'Scheme' = $Matches[2];
'Server' = $Matches[4];
'Path' = $Matches[5];
'QueryString' = $Matches[7];
'QueryStringParts' = @{}
}
foreach ($qs in $query_string.Split('&')) {
$qs_key, $qs_value = $qs.Split('=')
$url_parts.QueryStringParts.Add(
[System.Web.HttpUtility]::UrlDecode($qs_key),
[System.Web.HttpUtility]::UrlDecode($qs_value)
) | Out-Null
}
} else {
Throw [System.Management.Automation.ParameterBindingException] "Invalid URL Supplied"
}
Questo ti restituisce [hashtable]$url_parts
; che è uguale a ( Nota: gli spazi nelle parti complesse sono spazi ):
PS > $url_parts
Name Value
---- -----
Scheme https
Path /foos
Server example.vertigion.com
QueryString foo2=complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+bar%27%22&complex%3b%2f%3f%3a%40%26%3d%2b%24%2c+foo%27%22=bar2&foo1=bar1
QueryStringParts {foo2, complex;/?:@&=+$, foo'", foo1}
PS > $url_parts.QueryStringParts
Name Value
---- -----
foo2 complex;/?:@&=+$, bar'"
complex;/?:@&=+$, foo'" bar2
foo1 bar1