Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

URL Encoding Function


  • Please log in to reply
5 replies to this topic
DeathByNukes
  • Members
  • 43 posts
  • Last active: Nov 08 2013 02:58 AM
  • Joined: 25 Mar 2007
Here's a function I wrote that will URL encode a string. I wrote it because Titan's function was not working for me. Also, my function doesn't use regular expressions so I suspect it would perform better.

[Download]
UrlEncode( String )
{
	OldFormat := A_FormatInteger
	SetFormat, Integer, H

	Loop, Parse, String
	{
		if A_LoopField is alnum
		{
			Out .= A_LoopField
			continue
		}
		Hex := SubStr( Asc( A_LoopField ), 3 )
		Out .= "%" . ( StrLen( Hex ) = 1 ? "0" . Hex : Hex )
	}

	SetFormat, Integer, %OldFormat%

	return Out
}


trik
  • Members
  • 1317 posts
  • Last active: Jun 11 2010 11:48 PM
  • Joined: 15 Jul 2007
He made another one.
Religion is false. >_>

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Related:

URL_DONT_ESCAPE_EXTRA_INFO  = 0x02000000
URL_DONT_SIMPLIFY           = 0x08000000
URL_ESCAPE_PERCENT          = 0x00001000
URL_ESCAPE_SEGMENT_ONLY     = 0x00002000
URL_ESCAPE_SPACES_ONLY      = 0x04000000
URL_ESCAPE_UNSAFE           = 0x20000000
URL_INTERNAL_PATH           = 0x00800000
URL_PARTFLAG_KEEPSCHEME     = 0x00000001
URL_PLUGGABLE_PROTOCOL      = 0x40000000
URL_UNESCAPE                = 0x10000000
URL_UNESCAPE_HIGH_ANSI_ONLY = 0x00400000
URL_UNESCAPE_INPLACE        = 0x00100000


url1 := "test/t%e<s t.asp?url=/{ex% ample</abc.asp?frame=true#fr%agment"
url2 := "http://msdn.microsoft.com/en-us/library/ms776445%28VS.85%29.aspx"

flags := URL_ESCAPE_SPACES_ONLY | URL_DONT_ESCAPE_EXTRA_INFO | URL_ESCAPE_UNSAFE
MsgBox, 0, Escape,   % url1 "`n" UrlEscape( url1, flags )
MsgBox, 0, UnEscape, % url2 "`n" UrlUnEscape( url2, URL_UNESCAPE )


UrlEscape( url, flags ) {      ; www.msdn.microsoft.com/en-us/library/bb773774(VS.85).aspx
 VarSetCapacity( newUrl,500,0 ), pcche := 500
 DllCall( "shlwapi\UrlEscapeA", Str,url, Str,newUrl, UIntP,pcche, UInt,flags )
Return newUrl
}

UrlUnEscape( url, flags ) {    ; www.msdn.microsoft.com/en-us/library/bb773791(VS.85).aspx
 VarSetCapacity( newUrl,500,0 ), pcche := 500
 DllCall( "shlwapi\UrlUnescapeA", Str,url, Str,newUrl, UIntP,pcche, UInt,flags )
Return newUrl
}


Joolee
  • Members
  • 19 posts
  • Last active: Feb 17 2011 02:30 PM
  • Joined: 08 Aug 2005

Related:

URL_DONT_ESCAPE_EXTRA_INFO  = 0x02000000
URL_DONT_SIMPLIFY           = 0x08000000
URL_ESCAPE_PERCENT          = 0x00001000
URL_ESCAPE_SEGMENT_ONLY     = 0x00002000
URL_ESCAPE_SPACES_ONLY      = 0x04000000
URL_ESCAPE_UNSAFE           = 0x20000000
URL_INTERNAL_PATH           = 0x00800000
URL_PARTFLAG_KEEPSCHEME     = 0x00000001
URL_PLUGGABLE_PROTOCOL      = 0x40000000
URL_UNESCAPE                = 0x10000000
URL_UNESCAPE_HIGH_ANSI_ONLY = 0x00400000
URL_UNESCAPE_INPLACE        = 0x00100000


url1 := "test/t%e<s t.asp?url=/{ex% ample</abc.asp?frame=true#fr%agment"
url2 := "http://msdn.microsoft.com/en-us/library/ms776445%28VS.85%29.aspx"

flags := URL_ESCAPE_SPACES_ONLY | URL_DONT_ESCAPE_EXTRA_INFO | URL_ESCAPE_UNSAFE
MsgBox, 0, Escape,   % url1 "`n" UrlEscape( url1, flags )
MsgBox, 0, UnEscape, % url2 "`n" UrlUnEscape( url2, URL_UNESCAPE )


UrlEscape( url, flags ) {      ; www.msdn.microsoft.com/en-us/library/bb773774(VS.85).aspx
 VarSetCapacity( newUrl,500,0 ), pcche := 500
 DllCall( "shlwapi\UrlEscapeA", Str,url, Str,newUrl, UIntP,pcche, UInt,flags )
Return newUrl
}

UrlUnEscape( url, flags ) {    ; www.msdn.microsoft.com/en-us/library/bb773791(VS.85).aspx
 VarSetCapacity( newUrl,500,0 ), pcche := 500
 DllCall( "shlwapi\UrlUnescapeA", Str,url, Str,newUrl, UIntP,pcche, UInt,flags )
Return newUrl
}

Problem with this function is that it only converts to a maximum of "INTERNET_MAX_URL_LENGTH" characters. On my PC, that is 379 characters.

Precise
  • Members
  • 31 posts
  • Last active: May 23 2011 01:55 AM
  • Joined: 30 Dec 2007
Thanks for this. I'm sure I will find lots of uses for it. Here's a simple way to urlencode a search term for a url:

StringSplit, Segment, SearchTerm, %A_Space%
SearchTerm =
Loop, %Segment0%
{
Segment := UrlEncode(Segment%A_Index%)
If A_Index = 1
	SearchTerm := Segment
Else
	SearchTerm .= "+" . Segment	
}

Posted Image

jackieku
  • Members
  • 73 posts
  • Last active: Dec 26 2011 04:25 AM
  • Joined: 30 Nov 2008
I also make one for Unicode AutoHotkey.
According to the wiki, the URI must be encoded in UTF-8. (And as I know most web servers and browsers now use UTF-8.)
UrlEscapeA() API is not used because it isn't handles every case. The behavior of these functions are the same with the JavaScript's encodeURI() and encodeURIComponent() (checked with Firefox browser).
; JavaScript encodeURI()
URI_Encode(sURI, sExcepts = "!#$&'()*+,-./:;[email protected]_~")
{
	Transform sUTF8, ToCodePage, 65001, %sURI%
	origFmt := A_FormatInteger
	SetFormat IntegerFast, hex

	sResult := ""
	Loop
	{
		if (!(b := NumGet(sUTF8, A_Index - 1, "UChar")))
			break
		ch := Chr(b)
		; "is alnum" is not used because it is locale dependent.
		if (b >= 0x41 && b <= 0x5A ; A-Z
			|| b >= 0x61 && b <= 0x7A ; a-z
			|| b >= 0x30 && b <= 0x39 ; 0-9
			|| InStr(sExcepts, Chr(b), true))
			sResult .= Chr(b)
		else
		{
			ch := SubStr(b, 3)
			if (StrLen(ch) < 2)
				ch = "0" ch
			sResult .= "%" ch
		}
	}
	SetFormat IntegerFast, %origFmt%
	return sResult
}

; JavaScript encodeURIComponent()
URI_EncodeComponent(sURI, sExcepts = "!'()*-._~")
{
	return URI_Encode(sURI, sExcepts)
}

; Decode precent encoding
URI_Decode(sURI)
{
	Transform sUTF8, ToCodePage, 65001, %sURI%
	; #define URL_UNESCAPE_INPLACE 0x00100000
	; NOTE: UrlUnescapeW() doesn't work as expected.
	DllCall("shlwapi\UrlUnescapeA", "UIntPtr", &sUTF8, "UIntPtr", 0, "UIntPtr", 0, "UInt", 0x00100000)
	Transform sURI, FromCodePage, 65001, %sUTF8%
	return sURI
}