HttpObject info
by Joshua "RegularX" Birk · in Torque Game Builder · 05/19/2005 (4:02 pm) · 24 replies
I'm trying to create an http post request and store the response as a string. httpObject looks like it might be at least the start, but it's a bit arcane.
Unfortunately ... all the references to httpObject this site are locked out to us T2D only type people (according to google). Any way I can get some more information or example code on it's usage? It's event setup and the way it stores it's responses seems odd just by looking the code.
Unfortunately ... all the references to httpObject this site are locked out to us T2D only type people (according to google). Any way I can get some more information or example code on it's usage? It's event setup and the way it stores it's responses seems odd just by looking the code.
About the author
#2
edit the HTTPObject::expandPath function and remove this line:
Personally I use the TCPObject and handle any things... like the following I used to send an entire text file.
The caveat with the TCPOBject method is that you have to handle all URL encoding on your own.
05/21/2005 (1:30 pm)
%server = "localhost:80"; %path = "/website/"; %script = "testpage.php?var1=testing1&var2=testing2"; %hto = new HTTPObject(); %hto.get(%server, %path @ %script, "");
edit the HTTPObject::expandPath function and remove this line:
asciiEscapeTable['&'] = true;
Personally I use the TCPObject and handle any things... like the following I used to send an entire text file.
%obj = new TCPObject(TCPObj);
%obj.connect("www.yoursite.com:80");
function TCPObj::onConnected(%this)
{
// When connected to the server send large amount of data
// Read in large amount of text data
%filename= %largeTextFile;
%file = new FileObject();
if(%file.openForRead(%filename))
{
%text = %file.readLine();
while(!%file.isEOF())
{
%data = %data @ "{=||=}" @ %file.readLine(); // {=||=} is used to represent a CR and/or LF
// and should be parsed on the server side to
// such for an exact duplicate of the original
// data to be created
}
}
%file.delete();
// Replace SPC and + with HTML encoded alternative
%data=strreplace(%data," ","%20");
%data=strreplace(%data,"+","%2B");
//Create HTML variable to hold data
%data="data=" @ %data;
//Create HTML 1.0 POST line. Must do STRLEN of %data for Content-length HTML header
%htmlpost="POST /path/to/serverscript.php HTTP/1.0\nHost: www.yoursite.com:80\nUser-Agent: Torque/1.0 \nAccept: */*\nContent-length: " @ strlen(%data) @ "\nContent-type: application/x-www-form-urlencoded\n\n" @ %data;
//Send POST line to the webserver
%this.send(%htmlpost @ " \r\n");
}The caveat with the TCPOBject method is that you have to handle all URL encoding on your own.
#3
Maybe I'm being dense, but where are you processing the response? URLEncoding won't be problem, my POST is going to be fairly small and straightforward, but the response might be somewhat long.
05/21/2005 (1:48 pm)
Thanks Harold!Maybe I'm being dense, but where are you processing the response? URLEncoding won't be problem, my POST is going to be fairly small and straightforward, but the response might be somewhat long.
#4
05/21/2005 (9:49 pm)
In that example I don't.function HTTPObject::onLine( %this, %line )
{
echo(%line);
}
#5
I've slimmed it down to just this, since all I need to do is send a simple POST request:
The only echo I see, though, is the "Command Send" one.
05/28/2005 (1:06 pm)
Huh. Something doesn't seem to fly.I've slimmed it down to just this, since all I need to do is send a simple POST request:
$tcpserv = new TCPObject(TCPObj);
$tcpserv.connect("http://www.webcosa.com:80");
echo("Command Send");
function TCPObj::onConnected(%this)
{
echo("sending");
//Create HTML 1.0 POST line.
%htmlpost="POST /default.php HTTP/1.0\nHost: www.webcosa.com:80\nUser-Agent: Torque/1.0 \nAccept: */*\n";
//Send POST line to the webserver
%this.send(%htmlpost);
echo("sent");
}
function TCPObj::onLine( %this, %line )
{
echo(%line);
}
function TCPObj::onConnectFailed(%this)
{
echo("failed");
}The only echo I see, though, is the "Command Send" one.
#6
In the connect command you do not include the "http://" portion of the URL.
In the %htmlpost line you need to end the line with two \n
In the .send command you need to include a 'SPC "\r\n" '
05/28/2005 (2:04 pm)
You had a couple errors in your code:$tcpserv = new TCPObject();
$tcpserv.connect("www.webcosa.com:80");
echo("Command Send");
function TCPObject::onConnected(%this)
{
echo("sending");
//Create HTML 1.0 POST line.
%htmlpost="POST /default.php HTTP/1.0\nHost: www.webcosa.com:80\nUser-Agent: Torque/1.0\nAccept: */*\n\n";
//Send POST line to the webserver
%this.send(%htmlpost SPC "\r\n");
echo("sent");
}
function TCPObject::onConnectFailed(%this)
{
echo("Connection Failed");
}
function TCPObject::onLine(%this, %line)
{
echo(%line);
}In the connect command you do not include the "http://" portion of the URL.
In the %htmlpost line you need to end the line with two \n
In the .send command you need to include a 'SPC "\r\n" '
#7
05/28/2005 (2:13 pm)
Thanks! Updated, but I still only see the one echo.
#8
06/01/2005 (6:07 am)
Has anyone had either the http or tcpobj work from T2D itself? Is it perhaps not function in a T2D only environment? I keep poking at this but all I get is dead stick from it. It's like the object doesn't even try to get a socket running.
#9
06/01/2005 (6:13 am)
I think the Httpobject is part of the Torque 3d game elements and probably was not included in the T2d Layer. You could always purchase TGE and complie t2d into it, then you could use this class.
#10
Well everything should work that does in TGE. The only exception is that the 3D classes have been removed. Other than that, it's pretty much stock TGE with the demo scripts removed.
Well that's the theory.
- Melv.
06/01/2005 (6:14 am)
Joshua,Well everything should work that does in TGE. The only exception is that the 3D classes have been removed. Other than that, it's pretty much stock TGE with the demo scripts removed.
Well that's the theory.
- Melv.
#11
06/01/2005 (6:18 am)
So Melv, this should be working, right? I would expect it so, although would completely understand if it was mysteriously br0ked just due to the Early A status. Can someone double check for me? Hate to be a pain about this, but when I cut and paste Harold's code and it's still not working I get suspicious that there's much more I can do :) And it's the difference between me wrapping some existing code into a quick bundle or having to cobble together another socket library and jam that in.
#12
One thing though... HTTP POST operations require very specific header information, one of them being the content-length, some servers will respond if you don't have the header, others won't.
Change the post method to GET, and the code in my last post should work fine.
06/01/2005 (1:04 pm)
Yes it does work, I just tested it again.One thing though... HTTP POST operations require very specific header information, one of them being the content-length, some servers will respond if you don't have the header, others won't.
Change the post method to GET, and the code in my last post should work fine.
#13
06/01/2005 (1:13 pm)
I'll try that when I get home Harold ... but even if the POST header was the culprit, wouldn't I get echos from the onConnected event?
#14
Here is my client.cs file in it's entirety. I'm not getting any errors here. I can even type " $tcpserv.connect("www.webcosa.com:80"); " into the console and it doesn't error out, but there is no response either.
06/01/2005 (3:30 pm)
OK. I tried making just a barebones client.cs which would do nothing but this. I copied Harold's last post and changed POST to GET. I still seem to get no response at all, no activity, no events being fired, etc. I'm running OS X if that makes a difference and the latest version of the T2D build. I've modified the C++ a bit, but only by adding a few classes to the T2D package. Here is my client.cs file in it's entirety. I'm not getting any errors here. I can even type " $tcpserv.connect("www.webcosa.com:80"); " into the console and it doesn't error out, but there is no response either.
//-----------------------------------------------------------------------------
// Torque 2D.
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
// --------------------------------------------------------------------
// Initialise Client.
// --------------------------------------------------------------------
function initialiseClient()
{
// Initialise Base Client.
InitBaseClient();
// Key-Bindings.
GlobalActionMap.bind(keyboard, tilde, ToggleConsole);
GlobalActionMap.bind(keyboard, "escape", "quit()");
// Initialise Canvas.
InitCanvas("T2D");
if(isFullScreen()) { // for now, always play in window
toggleFullScreen();
}
// Load-up GUIs.
exec("./mainScreenGui.gui");
// Set GUI.
Canvas.setContent(mainScreenGui);
// Set Cursor.
Canvas.setCursor(DefaultCursor);
// Setup Scene.
setupT2DScene();
}
// --------------------------------------------------------------------
// Destroy Client.
//
// Here we destroy the SceneGraph.
// --------------------------------------------------------------------
function destroyClient()
{
// Destroy fxSceneGraph2D.
if ( isObject(t2dSceneGraph) )
t2dSceneGraph.delete();
}
// --------------------------------------------------------------------
// Setup T2D Scene.
// --------------------------------------------------------------------
function setupT2DScene()
{
// Create fxSceneGraph2D.
new fxSceneGraph2D(t2dSceneGraph);
// Associate Scenegraph with Window.
sceneWindow2D.setSceneGraph( t2dSceneGraph );
// Set Camera Position to be centered on (0,0) with
// view width/height of (100/80).
sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );
$tcpserv = new TCPObject();
$tcpserv.connect("www.webcosa.com:80");
echo("Command Send");
}
function TCPObject::onConnected(%this)
{
echo("sending");
//Create HTML 1.0 POST line.
%htmlpost="GET /default.php HTTP/1.0\nHost: www.webcosa.com:80\nUser-Agent: Torque/1.0\nAccept: */*\n\n";
//Send GET line to the webserver
%this.send(%htmlpost SPC "\r\n");
echo("sent");
}
function TCPObject::onConnectFailed(%this)
{
echo("Connection Failed");
}
function TCPObject::onLine(%this, %line)
{
echo(%line);
}
#15
- Melv.
06/02/2005 (12:20 am)
I'll admit to being a HTTP/Object virgin here so I can't really help much here. Harold's da-man here.- Melv.
#16
I guess I'll start putting logs into the C++ code to see where it's failing.
06/02/2005 (5:32 am)
I'm not entirely convinced it's on the TScript side of things, unless there's just something fundamental I'm missing. I more or less grok what Harold is doing here and I see where on C++ it should be flipping the events and then TCPObj's correlating con declarations .. but I'm just not getting anything :( I guess I'll start putting logs into the C++ code to see where it's failing.
#17
and I see "DNSResolved" in the console ... but then in TCPObject, I've got:
I don't see "DNSResolved Event". Same with any other TCPObject event. Unfortunately the event system is pretty over my head, so I'm at a complete standstill for figuring out why this wouldn't fire. Any suggestions?
06/02/2005 (5:12 pm)
OK, from the logs I've got running it seems like the connection to the socket is getting started, but none of the TCPObject events are getting fired after that. So I've got this in macCarbNet:conn->state = ConnectedNotifyEvent::DNSResolved;
Con::printf("DNSResolved");and I see "DNSResolved" in the console ... but then in TCPObject, I've got:
void TCPObject::onDNSResolved()
{
mState = DNSResolved;
Con::executef(this, 1, "onDNSResolved");
Con::printf("DNSResolved Event");
}I don't see "DNSResolved Event". Same with any other TCPObject event. Unfortunately the event system is pretty over my head, so I'm at a complete standstill for figuring out why this wouldn't fire. Any suggestions?
#18
07/11/2005 (1:52 pm)
Just an FYI. Just tried this code (TCP part) and works fine on windows xp/sp2...
#19
07/11/2005 (3:04 pm)
Yeah, my suspicion is that it's not operating for the mac build :(
#20
NetAddress netaddr;
connect(sock, &netaddr);
then in Net::connect we have
if(address->type != NetAddress::IPAddress)
return WrongProtocolType;
where address is netaddr. The problem is stringAddress (containing the url) from openConnectTo is never used. I don't know if this is in fact the problem or how to fix it if it is. however if this is it then i would guess it should be doing things simliar to the windows code. This occurs only on the Mac as this is in the file macCarbNet.cc
Update:
After a little more looking there is a lot of code that is not being compiled because it is surrounded by: #if later. Not sure if this means "fix this later" or "this stuff is done elsewhere later". In short im confused now.
07/11/2005 (3:21 pm)
I just had a quick look into this and it looks like the problem is in NetSocket Net::openConnectTo(const char *stringAddress). That function does this:NetAddress netaddr;
connect(sock, &netaddr);
then in Net::connect we have
if(address->type != NetAddress::IPAddress)
return WrongProtocolType;
where address is netaddr. The problem is stringAddress (containing the url) from openConnectTo is never used. I don't know if this is in fact the problem or how to fix it if it is. however if this is it then i would guess it should be doing things simliar to the windows code. This occurs only on the Mac as this is in the file macCarbNet.cc
Update:
After a little more looking there is a lot of code that is not being compiled because it is surrounded by: #if later. Not sure if this means "fix this later" or "this stuff is done elsewhere later". In short im confused now.
Torque Owner Joshua "RegularX" Birk