Sign in to follow this  
Followers 0
MiserableLife

[WinHTTP]dealing with non ASCII characters

3 posts in this topic

I'm having problems sending a string with non ASCII characters inside and I couldn't get the Content-length right in the headers.

Is there a way to encode the sting before I send the request?

Share this post


Link to post
Share on other sites



=o= " I don't know what sort of demo script should I post but I'll post parts of my script.

These are the functions that I think it may have the problem,

and I hope it's enough....

Func CreateResumableUpload($token,  $xml, $file_path)
    $tmp = StringSplit($file_path,'\')
    $file_name = $tmp[$tmp[0]]
    $file_size = FileGetSize($file_path)

    $yt_site = "uploads.gdata.youtube.com"
    $yt_path = "/resumable/feeds/api/users/default/uploads"

    $xml_length = 0
;~  $xml_length = StringLen($xml) ;#1 couldn't get the length right. 
;If it's too long, server(or maybe WinHTTP) will disconnect(timeout).
;If it's too short, server(or maybe WinHTTP) will ignore the extra bytes.
    $tmp = StringSplit($xml, '')
    For $i = 1 To $tmp[0]
        $tmp2 = StringToASCIIArray($tmp[$i], Default, Default,2)
        Switch $tmp2[0]
            Case 0 To 127
                $xml_length += 1
            Case 128 To 2047
                $xml_length += 2
            Case 2048 To 65535
                $xml_length += 3
            Case 65536 To 1114111
                $xml_length += 4
        EndSwitch
    Next
    ConsoleWrite($xml_length & @CRLF & @CRLF)

    $header = _
    'Accept: */*' & @CRLF & _
    'Authorization: GoogleLogin auth=' & $token & @CRLF & _
    'GData-Version: 2' & @CRLF & _
    'X-GData-Key: key=' & $dev_key & @CRLF & _
    'Content-Length: ' & $xml_length & @CRLF & _
    'X-Upload-Content-Type: video/*' & @CRLF & _
    'X-Upload-Content-Length: ' & $file_size & @CRLF & _
    'Content-Type: application/atom+xml; charset=UTF-8' & @CRLF & _
    'Slug: ' & $file_name & @CRLF & @CRLF

    $response = SendRequest("Post", $yt_site, $yt_path, $header, $xml)
    ConsoleWrite($response[0] & @CRLF & @CRLF)
    ConsoleWrite($response[1] & @CRLF & @CRLF)

    If StringInStr($response[0],"/resumableupload", 2) = 0 Then Return -1
    $pos = StringInStr($response[0],"/resumableupload", 2,1)
    $pos2 = StringInStr($response[0],@CRLF,2,1,$pos)
    Return StringMid($response[0],$pos,$pos2 - $pos)
EndFunc

; #FUNCTION# ;===============================================================================
;
; Name...........: _WinHttpWriteData
; Description ...: Writes request data to an HTTP server.
; Syntax.........: _WinHttpWriteData($hRequest, $data [, $iMode])
; Parameters ....: $hRequest - Valid handle returned by _WinHttpSendRequest().
;                  $data - Data to write.
;                  $iMode - Integer representing writing mode. Default is 0 - write ANSI string.
; Return values .: Success - Returns 1
;                          - @extended receives the number of bytes written.
;                          - Sets @error to 0
;                  Failure - Returns 0 and sets @error:
;                  |1 - DllCall failed.
; Author ........: trancexx, ProgAndy
; Modified.......:
; Remarks .......: $data variable is either string or binary data to write.
;                  $iMode can have these values:
;                  |0 - to write ANSI string
;                  |1 - to write binary data
; Related .......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa384120(VS.85).aspx
; Example .......; Yes
;
;==========================================================================================
Func _WinHttpWriteData($hRequest, $data, $iMode = 0)

    Local $iNumberOfBytesToWrite, $tData
    Switch $iMode
        Case 1
            $iNumberOfBytesToWrite = BinaryLen($data)
            $tData = DllStructCreate("byte[" & $iNumberOfBytesToWrite & "]")
        Case Else
            $iNumberOfBytesToWrite = StringLen($data)
            $tData = DllStructCreate("char[" & $iNumberOfBytesToWrite + 1 & "]")
    EndSwitch

    DllStructSetData($tData, 1, $data)

    Local $a_iCall = DllCall("winhttp.dll", "int", "WinHttpWriteData", _
            "hwnd", $hRequest, _
            "ptr", DllStructGetPtr($tData), _
            "dword", $iNumberOfBytesToWrite, _
            "dword*", 0)

    If @error Or Not $a_iCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    Return SetError(0, $a_iCall[4], 1)


EndFunc   ;==>_WinHttpWriteData

Here's the captured HTTP message(using #1):

POST /resumable/feeds/api/users/default/uploads HTTP/1.1
Accept: 
Accept: */*
Authorization: GoogleLogin auth=DQAAAJUAAACrdbcrZBmc-2Y-yJNT3eOd1VY9UOSIS1d-ZMWIgPMYsOxVcV_N0OKbpPRAvwRi-MunDJW2hvXhlSu2wruzQBkpwqmZ9P8KGHmlfCuPXOuoIelcR7eugjHgzhlDGySmV2ZaE_XXWcIVKJ-3Bl9COpxnRZQpxSRjKIN7xVLpeF9U1CkhR2fppmalDP5BRsm3wVxNoc0-R28lrMR5E9w-kPP_
GData-Version: 2
X-GData-Key: key=AI39si5XOGX3DxWiELQJf_-36sJB4MEWPcF4LjtUSh711r541DfHMNr771t4Q8eIiOv_ePhEGkbAc0hTEJzHF7O6OBUX-I8Y1Q
Content-Length: 422
X-Upload-Content-Type: video/*
X-Upload-Content-Length: 72155136
Content-Type: application/atom+xml; charset=UTF-8
Slug: M2U00802.MPG
User-Agent: Autoit v3.3.6.0;Youtube Uploader
Host: uploads.gdata.youtube.com
Connection: Keep-Alive

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"><media:group><media:title type="plain">..._.A...\.Y.. - </media:title><media:description type="plain"></media:description><media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category><media:keywords>..._.A...\.Y..</media:keywords></media:

Here's the server respond(using #1):

HTTP/1.1 400 Bad Request
Server: Upload Server Built on Apr 6 2010 11:54:18 (1270580058)
X-GData-User-Country: HK
Content-Type: application/vnd.google.gdata.error+xml
Date: Mon, 12 Apr 2010 18:53:10 GMT
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 279

<errors xmlns='http://schemas.google.com/g/2005'>
<error>
<domain>GData</domain>
<code>ParseException</code>
<internalReason>The element type &quot;media:group&quot; must be terminated by the matching end-tag &quot;&lt;/media:group&gt;&quot;.</internalReason>
</error>
</errors>

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
Sign in to follow this  
Followers 0