特殊字符作为URL中的参数传递

时间:2015-01-11 13:30:51

标签: regex powershell special-characters

要求:需要处理%&等特殊字符。需要调整下面的代码,以便通过$Control文件发送的特殊字符按原样处理。

例如:我在$control文件中有一个条目25% Dextrose(25ml)。我需要一种方法,以便$ie.Navigate只需导航到https://www.xxxy.com/search/all?name=25% Dextrose(25ml)。目前它被路由到https://www.xxxy.com/search/all?name=25%% Dextrose(25ml)(请注意URL中的额外%),因此找不到该网页。

**Few examples of special characters that need to be tackled:** 
'/' - 32care Mouth/Throat
'%' - 3d1% Gel(30g)
'&' - Accustix Glucose & Protein
'/' - Ace Revelol(25/(2.5mg)


function getStringMatch
     {
        # Loop through all 2 digit combinations in the $path directory
        foreach ($control In $controls)
        {
            $ie = New-Object -COMObject InternetExplorer.Application
            $ie.visible = $true
            $site = $ie.Navigate("https://www.xxxy.com/search/all?name=$control")
            $ie.ReadyState

            while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

            $link = $null
            $link = $ie.Document.get_links() | where-object {$_.innerText -eq "$control"}
            $link.click()

            while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

           $ie2 = (New-Object -COM 'Shell.Application').Windows() | ? {
           $_.Name -eq 'Windows Internet Explorer' -and $_.LocationName -match "^$control"
           }

            # NEED outerHTML of new page. CURRENTLY it is working for some.

            $ie.Document.body.outerHTML > d:\med$control.txt
        }
    }

    $controls = "Sporanox"

    getStringMatch

2 个答案:

答案 0 :(得分:3)

您想对URI进行URL编码。一开始就添加:

Add-Type -AssemblyName 'System.Web'

然后像这样对URL进行编码:

$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")

答案 1 :(得分:1)

正如Biffen所指出的,Web服务器会将特殊字符视为代码。因此,在您的情况下,需要修改$control,以便Web服务器了解您要去的位置。

解决此问题的一种方法是在您要查找的原始产品名称中查找特定字符,并将其替换为服务器可以理解的内容:

以下是整个代码:

function getStringMatch
{
    # Loop through all 2 digit combinations in the $path directory
    foreach ($control In $controls)
    {
        $ie = New-Object -COMObject InternetExplorer.Application
        $ie.visible = $true

        $s = $control -replace '%','%25'
        $s = $s -replace ' ','+'
        $s = $s -replace '&','%26'
        $s = $s -replace '/','%2F'
        $site = $ie.Navigate("https://www.xxxy.com/search/all?name=$s")

        while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

        $link = $null
        $link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
        $link.click()
        while ($ie.Busy){ sleep -Milliseconds 100 }

        $ie.Document.body.outerHTML > d:\TEMP\med$control.txt
    }
}

$controls = "Accustix Glucose & Protein"

getStringMatch

我尝试使用以下字符串:

"3d1% Gel(30g)"
"Ace Revelol(25/2mg)"
"Accustix Glucose & Protein"
"32care Mouth/Throat"