Jump to content

_EncodeUrl()


w0uter
 Share

Recommended Posts

Works on IE.

But does not work on firefox due to security reasons. :(

firefox sends you to the root.

ex: www.google.nl/firefox?client=firefox-a&rls=org.mozilla:nl-NL:official

would just go to: www.google.nl

Function:

Func _EncodeUrl($s_Url, $s_PreFix = 'http://')
    Local $s_NUrl = ''
    $s_Url = StringSplit($s_Url, '')
    
    For $i = 1 To $s_Url[0]
        If $s_Url[$i] = '/' OR $s_Url[$i] = '?' OR $s_Url[$i] = '=' Then 
            $s_NUrl &= $s_Url[$i]
            ContinueLoop
        EndIf
        $s_NUrl &= '%' & Hex(Asc($s_Url[$i]), 2)
    Next
    
    $s_NUrl = $s_PreFix & $s_NUrl
    
    Return $s_NUrl
    
EndFunc

example:

$Coded = _EnCodeUrl('www.google.nl/firefox?client=firefox-a&rls=org.mozilla:nl-NL:official')
ConsoleWrite($Coded & @CR)
Run('C:\Program Files\Internet Explorer\iexplore.exe ' & $Coded)

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

  • 1 month later...

It's not so bad... In fact, even though it didn't work for me how it seems to have worked for you, it's still a valuable reference on how to convert to Hexadecimal values.

Incase you're curious about the results I got, here's the breakdown:

  • Copied Func and Example directly from Topic into new AU3 Script; clean-up after Copy converted space-indents to tab-indents.
  • Commented out Run(), because I was only interested in the results, not the automatic launching of any program.
  • Commented out final assignment of $s_NUrl, because it's unnecessary the same result can be acheived by moving the right side of the assignment to the Return line, then enclosing the assignment inside brackets so that it's evaluated before the Return.
  • Save, then Go!
  • Result: http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%61/%66%69%72%65%66%6F%78?...
  • :evil: Theory: from CMD.EXE, did «START http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%61/%66%69%72%65%66%6F%78?...
  • Since Mozilla Firefox is registered in my system as the default browser, Firefox attempted to load the URL.
  • Result: «404: No Such Domain (Privoxy v3.0.3)» It was attempting to load from «%77%77%77%2e%67%6f%6f%67%6c%65%2e%63%61» :)
Possibly the results I got could be different if I wasn't browsing through a proxy, but at least I now have an example of how to convert to Hexadecimal values. And that will come in very useful in the future.

:D Possible improvements I can think of right now would be:

  • Make the Func more intelligent, so that it only converts characters that would need to be converted, and only characters that appear after the page being requested. (I know the latter would be somewhat difficult considering some websites that pass parameters in the format of a path, such as server.com/path/file.ext/some+var/opt_05=y)
  • I noticed that, when I changed the host part back to unencoded text and left everything after the "/" still encoded, the page loaded as expected. I would recommend instead of specifying some fixed value for the browser in the Run() function, use «@ComSpec & " /C " & Chr(34) & "START " & $Coded & Chr(34)», or somehow call the Windows® ShellExecute to launch the URL. Either one would allow the Script to load the URL in the system's default browser, no matter what it may be.
Here's my slightly modified copy of the Func:

CODE

Func _EncodeUrl($s_Url, $s_PreFix = 'http://')

Local $s_NUrl = ''

$s_Url = StringSplit($s_Url, '')

For $i = 1 To $s_Url[0]

If $s_Url[$i] = '/' OR $s_Url[$i] = '?' OR $s_Url[$i] = '=' Then

$s_NUrl = $s_NUrl & $s_Url[$i]

ContinueLoop

EndIf

$s_NUrl = $s_NUrl & '%' & Hex(Asc($s_Url[$i]), 2)

Next

;$s_NUrl = $s_PreFix & $s_NUrl

;Return $s_NUrl

Return ($s_PreFix & $s_NUrl)

EndFunc

;http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%61/%66%69%72%65%66%6F%78?%63%6C%69%65%6E%74=%66%69%72%65%66%6F%78%2D%61%26%72%6C%73=%6F%72%67%2E%6D%6F%7A%69%6C%6C%61%3A%65%6E%2D%43%41%3A%6F%66%66%69%63%69%61%6C

;http://www.google.ca/%66%69%72%65%66%6F%78?%63%6C%69%65%6E%74=%66%69%72%65%66%6F%78%2D%61%26%72%6C%73=%6F%72%67%2E%6D%6F%7A%69%6C%6C%61%3A%65%6E%2D%43%41%3A%6F%66%66%69%63%69%61%6C

$Coded = _EnCodeUrl('www.google.ca/firefox?client=firefox-a&rls=org.mozilla:en-CA:official')

ConsoleWrite($Coded & @CRLF)

;Run('C:\Program Files\Internet Explorer\iexplore.exe ' & $Coded)

Edited by 1of10

[right][img]style_emoticons/autoit/robot.gif[/img]One of TenSecondary Adjunct of Unimatrix Z03[/right]

Link to comment
Share on other sites

Since I'm feeling generous...

#Region _WebComposeURL()
; ===================================================================
; _WebComposeURL($sURL, $sLogin = "", $sPass = "", $sParams = "")
;
; Builds a valid URL from the four component parts.
; Parameters:
;   $sURL - IN - The URL use.
;   $sLogin - IN/OPTIONAL - The login to send with the URL.
;   $sPass - IN/OPTIONAL - The password to send with the URL (Requires a login name).
;   $sParams - IN/OPTIONAL - Optional parameters to send with the page.
; Returns:
;   A valid URI-encoded URL.
; ===================================================================
Func _WebComposeURL($sURL, $sLogin = "", $sPass = "", $sParams = "")
    Local Const $aURIValidChars[256] = _
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, _
        0,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0, _
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1, _
        0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0, _
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, _
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, _
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, _
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    Local $sFullURL
    Local $nPos = StringInStr($sURL, "://")
    If Not $nPos Then
        $sFullURL = "http://"
    Else
        $sFullURL = StringLeft($sURL, $nPos+2)
        $sURL = StringTrimLeft($sURL, $nPos+2)
    EndIf

    If $sLogin And StringLeft($sURL, StringLen($sLogin)) <> $sLogin Then
        $sFullURL &= $sLogin
        If $sPass Then $sFullURL &= ":" & $sPass
        $sFullURL &= "@"
    EndIf

    For $i = 1 To StringLen($sURL)
        Local $c = StringMid($sURL, $i, 1)
        If Number($aURIValidChars[Asc($c)]) Then
            $sFullURL &= $c
        Else
            $sFullURL &= StringFormat("%%%02X", Asc($c))
        EndIf
    Next

    If $sParams Then
        $sFullURL &= "?"
        For $i = 1 To StringLen($sParams)
            Local $c = StringMid($sParams, $i, 1)
            If $c = " " Then $c = "+"
            If Number($aURIValidChars[Asc($c)]) Then
                $sFullURL &= $c
            Else
                $sFullURL &= StringFormat("%%%02X", Asc($c))
            EndIf
        Next
    EndIf
    Return $sFullURL
EndFunc; _WebComposeURL()
#EndRegion _WebComposeURL()
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...