AzKay

URL Encoding

10 posts in this topic

Ive got this string: 123abc!@#$%^&*()_+

If I run it through a POST request on a website, it converts it to: 123abc%21%40%23%24%25%5E%26*%28%29_%2B+

What encoding is it?


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites



First I thought it was escape() or encodeURI() but they gave a slightly different output. But I guess you can make your own function in less than a minute since the output is % + hexcode of the character.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Had this snippet, don't know from where :), Kudos to the author!

ConsoleWrite(URLEncode("123abc!@#$%^&*()_+ ") & @crlf)


Func URLEncode($urlText)
    $url = ""
    For $i = 1 To StringLen($urlText)
        $acode = Asc(StringMid($urlText, $i, 1))
        Select
            Case ($acode >= 48 And $acode <= 57) Or _
                    ($acode >= 65 And $acode <= 90) Or _
                    ($acode >= 97 And $acode <= 122)
                $url = $url & StringMid($urlText, $i, 1)
            Case $acode = 32
                $url = $url & "+"
            Case Else
                $url = $url & "%" & Hex($acode, 2)
        EndSelect
    Next
    Return $url
EndFunc   ;==>URLEncode

Edit: I bet URL encoding is defined in some standard, but don't know which. I was the result of my function does not match yours to the point, * and _ didn't seem to be converted by your script (to emulate maybe explicitly exclude these characters from the URLEncode() function).

Edit2: Guess it's something like this http://en.wikipedia.org/wiki/Percent-encoding

Edited by KaFu

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I wrote some functions including UTF-8 conversion :(

Func _URIEncode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(BinaryToString(StringToBinary($sData,4),1),"")
    Local $nChar
    $sData=""
    For $i = 1 To $aData[0]
        ConsoleWrite($aData[$i] & @CRLF)
        $nChar = Asc($aData[$i])
        Switch $nChar
            Case 45, 46, 48-57, 65 To 90, 95, 97 To 122, 126
                $sData &= $aData[$i]
            Case 32
                $sData &= "+"
            Case Else
                $sData &= "%" & Hex($nChar,2)
        EndSwitch
    Next
    Return $sData
EndFunc

Func _URIDecode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(StringReplace($sData,"+"," ",0,1),"%")
    $sData = ""
    For $i = 2 To $aData[0]
        $aData[1] &= Chr(Dec(StringLeft($aData[$i],2))) & StringTrimLeft($aData[$i],2)
    Next
    Return BinaryToString(StringToBinary($aData[1],1),4)
EndFunc

MsgBox(0, '', _URIDecode(_URIEncode("testäöü fv")))

ProgAndy,

My question may sound stupid, but how do I know whether string needs to be decoded or encoded?

let me explain, I am taking part string from an URL box of IE automaticaly (Actually, its a value of one of the get method variables)

and wants to know if this string is decoded or encoded. E.g. Sometimes this values come in this form; %E0%E1%F8%E4%ED#876 and some times its just a UTF string (locale specific string that can be read by human).

For example:

http://www.robolo.co.il/SearchResults.html?cx=001994764102475033040%3A3o-v0ytfm9g&ie=windows-1255&cof=FORID%3A9&q=english#198

http://www.robolo.co.il/SearchResults.html?cx=001994764102475033040%3A3o-v0ytfm9g&ie=windows-1255&cof=FORID%3A9&q=%EE%F9%E4&x=45&y=15#892

Are there any functions such as IsEncodedURI or IsDecodedURI?

See the "q=" parameter

BTW: Your code is wonderful. Thanks for sharing it. >_<

Edited by lsakizada

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

Hi,

you should be able to decode any URL string and get the same result. Even if there are no encoded characters in there.

So to decode you can just call a decode function. No need to check with a IsEncoded function.

Greetings,

Manadar

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

ą, ń, ę, ż, dż, ź, ó, ł, ś, ć, ( from file ) ---> ±, ę, ż, dż, Ľ, ó, ł, ¶, ć, ( from url )

How to change this? : ±, ę, ż, dż, Ľ, ó, ł, ¶, ć, ---> ą, ń, ę, ż, dż, ź, ó, ł, ś, ć,

slawekc

The text is encoded in ISO8859-2. If you want, you can use the following function to get the text in ANSI (works only for Polish):

[ code='text' ] ( Popup )
Func ISOtoANSI($sText)
; http://en.wikipedia.org/wiki/Polish_code_pages
$sText = StringReplace($sText, Chr(Dec("A1")), "Ą")
$sText = StringReplace($sText, Chr(Dec("A6")), "Ś")
$sText = StringReplace($sText, Chr(Dec("AC")), "Ź")
$sText = StringReplace($sText, Chr(Dec("B1")), "ą")
$sText = StringReplace($sText, Chr(Dec("B6")), "ś")
$sText = StringReplace($sText, Chr(Dec("BC")), "ź")
Return $sText
EndFunc   ;==>ISOtoANSI
[indent=1]
Edited by tele123

Share this post


Link to post
Share on other sites

Here is my rulencode and urldecode functions. UTF8 is supported and functions are compliant with RFC 3986. The urlencode function accepts a seccond parameter - if true (default) it will encode spaces as plus. If false - the space will be encoded as "%20" (not compliant)

Func urlencode($str, $plus = True)
    Local $i, $return, $tmp, $exp
    $return = ""
    $exp = "[a-zA-Z0-9-._~]"
    If $plus Then
        $str = StringReplace ($str, " ", "+")
        $exp = "[a-zA-Z0-9-._~+]"
    EndIf
    For $i = 1 To StringLen($str)
        $tmp = StringMid($str, $i, 1)
        If StringRegExp($tmp, $exp, 0) = 1 Then
            $return &= $tmp
        Else
            $return &= StringMid(StringRegExpReplace(StringToBinary($tmp, 4), "([0-9A-Fa-f]{2})", "%$1"), 3)
        EndIf
    Next
    Return $return
EndFunc

Func urldecode($str)
    Local $i, $return, $tmp
    $return = ""
    $str = StringReplace ($str, "+", " ")
    For $i = 1 To StringLen($str)
        $tmp = StringMid($str, $i, 3)
        If StringRegExp($tmp, "%[0-9A-Fa-f]{2}", 0) = 1 Then
            $i += 2
            While StringRegExp(StringMid($str, $i+1, 3), "%[0-9A-Fa-f]{2}", 0) = 1
                $tmp = $tmp & StringMid($str, $i+2, 2)
                $i += 3
            Wend
            $return &= BinaryToString(StringRegExpReplace($tmp, "%([0-9A-Fa-f]*)", "0x$1"), 4)
        Else
            $return &= StringMid($str, $i, 1)
        EndIf
    Next
    Return $return
EndFunc

Share this post


Link to post
Share on other sites

babailiica,

This would have been best suited in the examples section and not digging up an old post such as this. Secondly, the best version I've seen has be ProgAndy's which features in the WinHTTP UDF. Thanks for your contribution and welcome to the Forum.

1 person likes this

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
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