Page 1 of 1

WinAPI: HttpSendRequestW_() is not a function

Posted: Wed Feb 05, 2020 4:24 pm
by Derren
Hi everyone,

how can I use the function HttpSendRequestW_()

It is referenced on the MSDN of HttpSendRequestA() https://docs.microsoft.com/en-us/window ... ndrequesta
And also on this forum post https://social.msdn.microsoft.com/Forum ... vclanguage

When I try to call it, PB just tells me it's not a function, array etc.

I'm using PureBasic 5.71 LTS (Windows - x64), Windows 10.

Can I use an import command? I'm really not that firm with this kind of thing. One of the reason I love PB is the direct Api-Access.

The problem I'm having is, the same as in the forum post. Because strings are internally stored in unicode, I can't seem to send them with this function. Only the first character gets sent.
I did find the Ascii()-function in the help, so that's what I'm using meanwhile. But I can't be 100% certain that I will never want to send some unicode characters (emojis etc).

Thanks in advance :)

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Thu Feb 06, 2020 8:30 am
by Shardik
The following example imports HttpSendRequestW() (tested successfully on Windows 10 V1809 x64 with PB 5.71 x86 and x64):

Code: Select all

Import "WinInet.Lib"
  HttpSendRequestW(RequestHandle.I, Headers.S, HeadersLength.L, *OptionalData,
    OptionalDataLength.L)
EndImport

Debug HttpSendRequestW(0, "", 0, 0, 0)

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Thu Feb 06, 2020 12:01 pm
by Derren
Thank you for the code,
but then the issue must lie elsewhere.

The two first lines only send the first character of my payload, while the 3rd works properly.

Code: Select all

SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, @Body$, Len(Body$)) ;only first char is sent
SendHandle = HttpSendRequestW(*Info\RequestHandle, #Null$, 0, @Body$, Len(Body$)) ;only first char is sent
SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, Ascii(Body$), Len(Body$)) ;full Body$ is sent to server
I'm using infratec's code here, which also handles the opening of the connection and the sending of headers and receiving of answers: viewtopic.php?p=441105#p441105

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Sat Feb 08, 2020 11:27 pm
by firace
Derren wrote:Thank you for the code,
but then the issue must lie elsewhere.

The two first lines only send the first character of my payload, while the 3rd works properly.

Code: Select all

SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, @Body$, Len(Body$)) ;only first char is sent
SendHandle = HttpSendRequestW(*Info\RequestHandle, #Null$, 0, @Body$, Len(Body$)) ;only first char is sent
SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, Ascii(Body$), Len(Body$)) ;full Body$ is sent to server
I'm using infratec's code here, which also handles the opening of the connection and the sending of headers and receiving of answers: viewtopic.php?p=441105#p441105
The usual encoding to use in HTTP requests is UTF8, which has good support for both ASCII and wide characters. So I would use:

Code: Select all

*d = UTF8(body$)
SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, *d, MemorySize(*d) ) ;

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Thu Mar 12, 2020 10:30 am
by Derren
Okay, so I tried

Code: Select all

*d = UTF8(body$)
SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null, 0, *d, MemorySize(*d) ) ;
But Sharepoint (Microsoft Sharepoint, the REST endpoint I'm trying to trigger) doesn't like it for some reason.
It gives me an error that there are more than one JSON-Root (the body is JSON and the same body works in Ascii (with no Umlauts))
But since I can't see anything wrong with the transmitted data (I tried sending the same request to my own webserver so I can see what is really sent over the wire) and JSON-Lint said it's valid, so I have no idea what Microsoft's Sharepoint is not liking...

Interestingly enough, the response (in German) had an "ΓΌ" in it and it was escaped with "\u00fc", so that's what I'm using now and that works.

I'm using

Code: Select all

SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null$, 0, Ascii(Body$), Len(Body$))
and replace all Umlauts with their \u-escape code.

Thanks for all your input. Really appreciated :)

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Thu Mar 12, 2020 11:44 pm
by mk-soft
Don't forget free memory for ASCII and UTF8 functions ...

Code: Select all

;*Body = Ascii(Body$)
*Body = UTF8(Body$)
SendHandle = HttpSendRequest_(*Info\RequestHandle, #Null$, 0, *Body, MemorySize(*Body)
FreeMemory(*Body)

Re: WinAPI: HttpSendRequestW_() is not a function

Posted: Fri Mar 13, 2020 11:54 am
by Derren
Thanks, I'll add this to the code.
Right now there are only 2 requests and then the program ends, but that's no reason to ommit such important commands.