Jump to content

Send UTF-8/Unicode string using the WinHttp UDF


Recommended Posts

Hi everyone!

I'm trying to send a string (Filename in Arabic) as a post parameter to PHP using the WinHttp UDF. The problem i'm facing is that, at the server end, the string shows up as "Question Marks" and not in Arabic. 

However, if i send this string using the Winhttp Objects, it shows up properly in Arabic.

Is there any setting in the WinHttp UDF that i need to set before sending the post request, something that would send the post parameter as a UTF-8 string? The request being sent is over SSL, and im using the _WinHttpSimpleSendSSLRequest function.

here's the code im using.

#NoTrayIcon
#include<Winhttp.au3>

$domain = "someserver.com"
$sPath = "/utf-echo.php"

Global $ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
$utfText = "مرحبا بالعالم.docx"
$postData = "utf=" & $utfText

;Using Winhttp Object
_httpRequestObj("https://" & $domain & $sPath ,'POST', $postData, $ua)

;Using Winhttp UDF
$hOpen = _WinHttpOpen($ua)
$hConnect = _WinHttpConnect($hOpen, $domain)
$hRequest = _WinHttpSimpleSendSSLRequest($hConnect, "POST", $sPath, Default, $postData, Default)


Func _httpRequestObj($url, $method, $postData, $ua)
    $oHTTP = ObjCreate('WinHTTP.WinHTTPRequest.5.1')
    $method = StringLower($method)
    Local $oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
    $oHTTP.Open($method, $url, False)
    $oHTTP.SetRequestHeader('User-Agent', $ua)
    $oHTTP.Option(4) = 13056
    $oHTTP.Option(9) = 168
    If $method = 'GET' Then
        $oHTTP.Send()
    Else
        $oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        $oHTTP.SetRequestHeader('Content-Length', StringLen($postData))
        $oHTTP.Send($postData)
    EndIf
    $oHTTP.WaitForResponse
    Local $ret[2]
    $ret["0"] = $oHTTP.Status
    $ret["1"] = $oHTTP.Responsetext
    Return $ret
EndFunc   ;==>_ProxyhttpRequest

Output at the server end:

postString.PNG.637f162ce3fe7abe8d67d9a1bb4521b1.PNG

Hope this explains the situation! Any help would be appreciated!

thanks!!

 

 

Link to comment
Share on other sites

Convert $postdata (native UTF16-LE string) to UTF8 using _WinAPI_WideCharToMultiByte()

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd Great!, thanks! this worked for me!

_WinAPI_WideCharToMultiByte($utfText,65001)

Although, i'd still be interested to know how this would work while uploading a file using _WinhttpSimpleFormFill() function from the Winhttp UDF... Any tips?

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...