Http is just TCP/IP on port 80
for example:
"Open www.google.com 80"
will open a Http connection to www.google.com.
Twitter actually requires more of the Http protocol than google.
For example, the "Host" field is often required in case there's more than one
domain name mapped to the server's IP address so it can tell which
website you actually want.
Twitter also requires a final linefeed and carriage return ("\r\n")
"GET /\n"
"Host: server\r\n"
"\r\n"
/*
Parameters: The server to telnet into, the get command that needs to be sent, a custom HtmlParser that
is called every time a character is received. The parser is responsible for processing the HTML
that is returned.
*/
bool WiFly::HttpWebRequest(const char* server, const char* getCommand, HtmlParser* parser)
{
m_printer->println(getCommand);
FlushRX();
FlushRX();
// Enter command mode
EnterCommandMode();
FlushRX();
// open a TCP connection, port 80 for HTTP
WriteToWiFly("open ");
WriteToWiFly(server);
WriteToWiFlyCR(" 80");
bool openOK = WaitUntilReceived(COMM_OPEN);
if (openOK == false)
{
m_printer->println("open port failed!");
delay(1000);
WriteToWiFlyCR("close");
WaitUntilReceived(COMM_CLOSE);
ExitCommandMode();
return false;
}
// eg. "GET /search.json?q=foo HTTP/1.1\r\n"
WriteToWiFlyCRLF(getCommand);
// eg. "Host: search.twitter.com\r\n"
WriteToWiFly("Host: ");
WriteToWiFlyCRLF(server);
// "\r\n"
WriteToWiFlyCRLF("");
// now wait for the response
int timeOut = 0;
bool ok = false;
while(timeOut < 5000)// timeout after 5 seconds
{
if((ReadCharFromWiFly(LSR) & 0x01))
{
char incoming_data = ReadCharFromWiFly(RHR);
m_printer->print(incoming_data,BYTE);
bool done = parser->Parse(incoming_data);
if (done)
{
ok = true;
break;
}
timeOut = 0; //reset the timeout
}
else
{
delay(1);
timeOut++;
}
}
FlushRX();
// disconnect TCP connection.
WriteToWiFlyCR("close");
WaitUntilReceived(COMM_CLOSE);
ExitCommandMode();
return ok;
}