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!).
webservice.cpp
- Committer:
- hlipka
- Date:
- 2011-01-29
- Revision:
- 1:62e112d14c8f
- Parent:
- 0:5e8527b638e1
- Child:
- 2:687430e7f63a
File content as of revision 1:62e112d14c8f:
#include "Timer.h" #include "webservice.h" #include "spxmlnode.hpp" #include "spxmlhandle.hpp" WebService::WebService(const char* urlStr) { Url *url=new Url(); url->fromString(urlStr); // printf("host name from URL=%s\n",url->getHost().c_str()); _stream=new TCPLineStream(url->getHost().c_str(),0==url->getPort()?80:url->getPort(),"\r\n"); delete url; _request=string("GET ").append(urlStr).append(" HTTP/1.0\r\n"); // printf("request=[%s]\n",_request.c_str()); } SP_XmlDomParser* WebService::callService() { if (!_stream->open()) return NULL; // printf("send request[%s]\n",request.c_str()); _stream->sendLine(_request); string firstLine=_stream->readLine(); // todo: parse for HTTP response // response must be for HTTP/1.0, and be 200 if (0!=firstLine.compare("HTTP/1.0 200 OK")) { printf("call not sucessfull, response=%s\n",firstLine.c_str()); return NULL; } // skip headers while (true) { string line=_stream->readLine(); // printf("header=[%s]\n",line.c_str()); if (0==line.length()) break; } SP_XmlDomParser *parser=new SP_XmlDomParser(); while (true) { string line=_stream->readLine(1500); // printf("content=[%s]\n",line.c_str()); parser->append(line.c_str(),line.length()); if (0==line.length()) break; } _stream->close(); return parser; } void WebService::close() { if (NULL!=_stream) { _stream->close(); delete _stream; _stream=NULL; } }