Sök…


Anmärkningar

Det reguljära uttrycket som användes i exemplen på avkodnings-URL har tagits från RFC 2396, bilaga B: Analys av en URI-referens med ett regelbundet uttryck ; för eftertiden, här är en offert:

Följande rad är det regelbundna uttrycket för att dela upp en URI-referens till dess komponenter.

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
 12            3  4          5       6  7        8 9

Siffrorna i den andra raden ovan är endast för att underlätta läsbarheten. de anger referenspunkterna för varje subexpression (dvs varje parad parentes). Vi refererar till det värde som matchas för subexpression som $. Att till exempel matcha ovanstående uttryck till

http://www.ics.uci.edu/pub/ietf/uri/#Related

resulterar i följande subexpression-matchningar:

$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

Snabbstart: Kodning

$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

Obs: Mer information om HTTPUtility .

Snabbstart: Avkodning

Obs: dessa exempel använder variablerna som skapats i avsnittet Snabbstart: kodning ovan.

# 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

Obs: Mer information om HTTPUtility .

Koda frågesträng med `[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 '&'))

Med [uri]::EscapeDataString() kommer du att märka att apostrofen ( ' ) inte kodades:

https://example.vertigion.com/foos ? foo2 = komplex% 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

Koda frågesträng med `[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 '&'))

Med [System.Web.HttpUtility]::UrlEncode() kommer du att märka att utrymmen förvandlas till plustecken ( + ) istället för %20 :

https://example.vertigion.com/foos ? foo2 = komplex% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 & komplex% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + foo% 27% 22 = bar2 & foo1 = bar1

Avkoda URL med `[uri] :: UnescapeDataString ()`

Kodad med [uri]::EscapeDataString()

Först avkodar vi URL och Query String kodad med [uri]::EscapeDataString() i exemplet ovan:

https://example.vertigion.com/foos ? foo2 = komplex% 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 = '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"
}

Detta ger dig tillbaka [hashtable]$url_parts ; vilket är lika ( Obs: utrymmena i de komplexa delarna är utrymmen ):

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

Kodad med [System.Web.HttpUtility]::UrlEncode()

Nu avkodar vi URL och frågesträngen kodad med [System.Web.HttpUtility]::UrlEncode() i exemplet ovan:

https://example.vertigion.com/foos ? foo2 = komplex% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 & komplex% 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"
}

Detta ger dig tillbaka [hashtable]$url_parts , vilket är lika ( Obs: utrymmena i de komplexa delarna är plustecken ( + ) i den första delen och utrymmena i den andra delen):

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

Avkoda URL med `[System.Web.HttpUtility] :: UrlDecode ()`

Kodad med [uri]::EscapeDataString()

Först avkodar vi URL och Query String kodad med [uri]::EscapeDataString() i exemplet ovan:

https://example.vertigion.com/foos ? foo2 = komplex% 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 = '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"
}

Detta ger dig tillbaka [hashtable]$url_parts ; vilket är lika ( Obs: utrymmena i de komplexa delarna är utrymmen ):

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

Kodad med [System.Web.HttpUtility]::UrlEncode()

Nu avkodar vi URL och frågesträngen kodad med [System.Web.HttpUtility]::UrlEncode() i exemplet ovan:

https://example.vertigion.com/foos ? foo2 = komplex% 3b% 2f% 3f% 3a% 40% 26% 3d% 2b% 24% 2c + bar% 27% 22 & komplex% 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"
}

Detta ger dig tillbaka [hashtable]$url_parts ; vilket är lika ( Obs: utrymmena i de komplexa delarna är utrymmen ):

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


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow