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
00001 /* 00002 * WebService library 00003 * Copyright (c) 2010 Hendrik Lipka 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy 00006 * of this software and associated documentation files (the "Software"), to deal 00007 * in the Software without restriction, including without limitation the rights 00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 * copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in 00013 * all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 * THE SOFTWARE. 00022 */ 00023 00024 #include "Timer.h" 00025 00026 #include "webservice.h" 00027 00028 #include "spxmlnode.hpp" 00029 #include "spxmlhandle.hpp" 00030 00031 WebService::WebService(const char* urlStr) { 00032 Url *url=new Url(); 00033 url->fromString(urlStr); 00034 00035 // printf("host name from URL=%s\n",url->getHost().c_str()); 00036 00037 _stream=new TCPLineStream(url->getHost().c_str(),0==url->getPort()?80:url->getPort(),"\r\n"); 00038 delete url; 00039 00040 _request=string("GET ").append(urlStr).append(" HTTP/1.0\r\n"); 00041 // printf("request=[%s]\n",_request.c_str()); 00042 } 00043 00044 SP_XmlDomParser* WebService::callService() { 00045 if (!_stream->open()) 00046 return NULL; 00047 00048 // printf("send request[%s]\n",request.c_str()); 00049 _stream->sendLine(_request); 00050 00051 string firstLine=_stream->readLine(); 00052 // todo: parse for HTTP response 00053 // response must be for HTTP/1.0, and be 200 00054 if (0!=firstLine.compare("HTTP/1.0 200 OK")) { 00055 printf("call not sucessfull, response=%s\n",firstLine.c_str()); 00056 return NULL; 00057 } 00058 // skip headers 00059 while (true) { 00060 string line=_stream->readLine(); 00061 // printf("header=[%s]\n",line.c_str()); 00062 if (0==line.length()) 00063 break; 00064 } 00065 SP_XmlDomParser *parser=new SP_XmlDomParser(); 00066 while (true) { 00067 string line=_stream->readLine(1500); 00068 // printf("content=[%s]\n",line.c_str()); 00069 parser->append(line.c_str(),line.length()); 00070 if (0==line.length()) 00071 break; 00072 } 00073 _stream->close(); 00074 return parser; 00075 } 00076 00077 void WebService::close() { 00078 if (NULL!=_stream) { 00079 _stream->close(); 00080 delete _stream; 00081 _stream=NULL; 00082 } 00083 } 00084
Generated on Wed Jul 13 2022 10:40:59 by 1.7.2