What is the best way, in my game, to HTTP POST to an URL? I just need to send a single binary file, and I will get back a single number as text, which I need to check.
Does it have to be done stand-alone? If not, I'd suggest writing a RAPI module to do it while syncing. That way you can use all the standard desktop stuff for the networking, which might be a little easier.
Actually, attached is a piece of code I came into some time ago... If I remember it correctly, it reads a web site and displays the HTML, all on the PPC. I know this isn't what your looking for exactly, but I suspect it will put you on the right track as far as what classes/API's to use.
You should be able to use the InternetXXX functions.
I am using this in my highscore reporting system but I only use InternetOpenUrl.
OK I got this working using wininet.
I used HttpSendRequest and was able to successfully POST my binary file.
I can get the return code 200 (success) from the server using HttpQueryInfo.
But how do I get the returned text that the server served up? Update: Just found it, InternetReadFile works fine.
My high scores uploading mechanism is working now, I just have to add the UI and make sure all errors are handled.
I haven't had a chance to try this, since I'm at work, but is it really just a matter of calling the following functions to send a request to a web server?
InternetOpen( ... )
InternetOpenUrl( ... )
InternetCloseHandle( ... )
What happens if there's no internet connection available? I see that the InternetOpen function returns NULL, but how long does it take for that to happen?
Thanks, John
I'm using a slightly different sequence of functions, but yes that's how it seems to work. You check error codes and other error information.
I haven't tested it yet when it fails, but hopefully it won't take too long to return an error code.
HttpSendRequest seems to take about 20s to fail if I opened an incorrect IP address. It seems fairly quick.
Please take a moment to test my high scores upload. Concentrix BETA7 is available here:
www.scalenesoftware.com/forums/
Here's a question I have.
The documentation says you can reuse a request for multiple HttpSendRequest so long as you read all the returned data.
I found that didn't work for me, so I did an HttpOpenRequest for each HttpSendRequest.
But I know it should work. I'm just unclear on exactly what must be read. The documentation is unclear, what do they mean?
You might want to look into the KEEP_ALIVE flag. I'm too tired at the moment to look up where it goes though...
-John
It seems to be working now so I probably won't touch it till people have problems with it. It's just that in my heart I know I should be reusing a connection if I can, since it's wasteful otherwise. I just wish the docs were clearer on exactly what to do to reuse a request, instead of just hinting at it as if my name were Tantalus.
First of all, it still wasn't working. It only seemed like it was because of another bug.
Second, the problem of encoding the binary data reared its ugly head. I still couldn't get it to work using application/x-www-form-urlencoded, even though it posted fine using curl (which oddly enough says it doesn't urlencode for you). I got it to post OK, but it wasn't decoded on the other end, so I gave up.
I switch to use multipart/form-data message type, and just dumped the binary file in there. That''s the type I should have been using from the first anyways, it's more appropriate for binary data. I thought it would be more difficult to implement but it wasn't really.