WinAPI: HttpSendRequestW_() is not a function

Windows specific forum
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

WinAPI: HttpSendRequestW_() is not a function

Post 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 :)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: WinAPI: HttpSendRequestW_() is not a function

Post 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)
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: WinAPI: HttpSendRequestW_() is not a function

Post 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
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: WinAPI: HttpSendRequestW_() is not a function

Post 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) ) ;
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: WinAPI: HttpSendRequestW_() is not a function

Post 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 :)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: WinAPI: HttpSendRequestW_() is not a function

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: WinAPI: HttpSendRequestW_() is not a function

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