A simple web service over HTTP library. Calls a HTTP server via GET, and returns the response wrapped in a XML parser. All calls are synchronous. Needs the NetServicesMin, DNSResolver, TcpLineStream and spxml libraries. The code for URL handling has been copied directly from the original NetServices library (Thanks to Donatien!).

Committer:
hlipka
Date:
Sat Jan 29 23:29:41 2011 +0000
Revision:
1:62e112d14c8f
Parent:
0:5e8527b638e1
Child:
2:687430e7f63a
Changed to use the TcpLineStream library. This makes the code much simpler.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 1:62e112d14c8f 1 #include "Timer.h"
hlipka 1:62e112d14c8f 2
hlipka 1:62e112d14c8f 3 #include "webservice.h"
hlipka 1:62e112d14c8f 4
hlipka 1:62e112d14c8f 5 #include "spxmlnode.hpp"
hlipka 1:62e112d14c8f 6 #include "spxmlhandle.hpp"
hlipka 1:62e112d14c8f 7
hlipka 1:62e112d14c8f 8 WebService::WebService(const char* urlStr) {
hlipka 1:62e112d14c8f 9 Url *url=new Url();
hlipka 1:62e112d14c8f 10 url->fromString(urlStr);
hlipka 1:62e112d14c8f 11
hlipka 1:62e112d14c8f 12 // printf("host name from URL=%s\n",url->getHost().c_str());
hlipka 1:62e112d14c8f 13
hlipka 1:62e112d14c8f 14 _stream=new TCPLineStream(url->getHost().c_str(),0==url->getPort()?80:url->getPort(),"\r\n");
hlipka 1:62e112d14c8f 15 delete url;
hlipka 1:62e112d14c8f 16
hlipka 1:62e112d14c8f 17 _request=string("GET ").append(urlStr).append(" HTTP/1.0\r\n");
hlipka 1:62e112d14c8f 18 // printf("request=[%s]\n",_request.c_str());
hlipka 1:62e112d14c8f 19 }
hlipka 1:62e112d14c8f 20
hlipka 1:62e112d14c8f 21 SP_XmlDomParser* WebService::callService() {
hlipka 1:62e112d14c8f 22 if (!_stream->open())
hlipka 1:62e112d14c8f 23 return NULL;
hlipka 1:62e112d14c8f 24
hlipka 1:62e112d14c8f 25 // printf("send request[%s]\n",request.c_str());
hlipka 1:62e112d14c8f 26 _stream->sendLine(_request);
hlipka 1:62e112d14c8f 27
hlipka 1:62e112d14c8f 28 string firstLine=_stream->readLine();
hlipka 1:62e112d14c8f 29 // todo: parse for HTTP response
hlipka 1:62e112d14c8f 30 // response must be for HTTP/1.0, and be 200
hlipka 1:62e112d14c8f 31 if (0!=firstLine.compare("HTTP/1.0 200 OK")) {
hlipka 1:62e112d14c8f 32 printf("call not sucessfull, response=%s\n",firstLine.c_str());
hlipka 1:62e112d14c8f 33 return NULL;
hlipka 1:62e112d14c8f 34 }
hlipka 1:62e112d14c8f 35 // skip headers
hlipka 1:62e112d14c8f 36 while (true) {
hlipka 1:62e112d14c8f 37 string line=_stream->readLine();
hlipka 1:62e112d14c8f 38 // printf("header=[%s]\n",line.c_str());
hlipka 1:62e112d14c8f 39 if (0==line.length())
hlipka 1:62e112d14c8f 40 break;
hlipka 1:62e112d14c8f 41 }
hlipka 1:62e112d14c8f 42 SP_XmlDomParser *parser=new SP_XmlDomParser();
hlipka 1:62e112d14c8f 43 while (true) {
hlipka 1:62e112d14c8f 44 string line=_stream->readLine(1500);
hlipka 1:62e112d14c8f 45 // printf("content=[%s]\n",line.c_str());
hlipka 1:62e112d14c8f 46 parser->append(line.c_str(),line.length());
hlipka 1:62e112d14c8f 47 if (0==line.length())
hlipka 1:62e112d14c8f 48 break;
hlipka 1:62e112d14c8f 49 }
hlipka 1:62e112d14c8f 50 _stream->close();
hlipka 1:62e112d14c8f 51 return parser;
hlipka 1:62e112d14c8f 52 }
hlipka 1:62e112d14c8f 53
hlipka 1:62e112d14c8f 54 void WebService::close() {
hlipka 1:62e112d14c8f 55 if (NULL!=_stream) {
hlipka 1:62e112d14c8f 56 _stream->close();
hlipka 1:62e112d14c8f 57 delete _stream;
hlipka 1:62e112d14c8f 58 _stream=NULL;
hlipka 1:62e112d14c8f 59 }
hlipka 1:62e112d14c8f 60 }
hlipka 1:62e112d14c8f 61