Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
http_helper_getstring.cpp

Accessing any server using http(s)

#include <iostream>
#include "msparser.hpp"
using namespace matrix_science;
int main(int argc, char ** argv)
{
std::string url, action, httpUserName, httpPassword, username, password;
if (argc == 3 || argc == 5 || argc == 7)
{
url = argv[1];
action = argv[2];
if(argc == 5 || argc == 7)
{
httpUserName = argv[3];
httpPassword = argv[4];
}
if (argc == 7)
{
username = argv[5];
password = argv[6];
}
}
else
{
std::cerr << "Usage: " << argv[0] << " <url> <action> [httpUserName] [httpPassword] [username] [password]" << std::endl;
return 1;
}
std::cout << "url: " << url << " action: " << action;
if(httpUserName != "")
{
std::cout << " httpUserName: " << httpUserName << " httpPassword: " << httpPassword;
}
if(username != "")
{
std::cout << " username: " << username << " password: " << password;
}
std::cout << std::endl;
ms_connection_settings connectionSettings;
connectionSettings.setUserAgent("CppTest/1.0" + connectionSettings.getUserAgent());
if(httpUserName != "")
{
connectionSettings.setHttpUsername(httpUserName);
}
if(httpPassword != "")
{
connectionSettings.setHttpPassword(httpPassword);
}
ms_http_helper httpHelper(url, connectionSettings);
if(!httpHelper.isValid())
{
std::cout << "Error: " << httpHelper.getLastErrorString() << std::endl;
for(int i = 0 ; httpHelper.getErrorHandler() && i < httpHelper.getErrorHandler()->getNumberOfErrors() ; i++ )
{
std::cout << "Error number: " << httpHelper.getErrorHandler()->getErrorNumber(i) << " (" << httpHelper.getErrorHandler()->getErrorRepeats(i) << " times) : "
<< httpHelper.getErrorHandler()->getErrorString(i) << std::endl;
}
std::cout << std::endl;
return -1;
}
std::string httpGETString;
ms_http_helper_return helperReturn_string(httpHelper.httpGetString(action, httpGETString));
if(helperReturn_string.isOk())
{
std::cout << "httpGETString - OK: " << std::endl << httpGETString << std::endl;
}
else
{
std::cout << "httpGETString - Error: " << helperReturn_string.getErrorText() << " (" << helperReturn_string.getHttpStatusCode() << ")" << std::endl;
}
std::string header;
ms_http_helper_return helperReturn_header(httpHelper.httpGetHeader("", header));
if(helperReturn_header.isOk())
{
std::cout << "httpGetHeader - OK : " << std::endl << header << std::endl;
}
else
{
std::cout << "httpGetHeader - Error: " << helperReturn_header.getErrorText() << " (" << helperReturn_header.getHttpStatusCode() << ")" << std::endl;
}
ms_http_helper_return helperReturn_buffer(httpHelper.httpBufferedOpen(""));
if(helperReturn_buffer.isOk())
{
std::cout << "httpBufferedOpen - OK :" << std::endl;
std::string buffer;
while (true)
{
ms_http_helper_return result = httpHelper.httpBufferedGetString(buffer, 10000);
if (buffer.length() == 0 || !result.isOk())
{
break;
}
std::cout << buffer;
}
httpHelper.httpBufferedClose();
}
else
{
std::cout << "httpBufferedOpen - Error: " << helperReturn_buffer.getErrorText() << " (" << helperReturn_buffer.getHttpStatusCode() << ")" << std::endl;
}
std::cout << std::endl;
return 0;
}
Settings required to make an HTTP connection to a Mascot server.
Definition: ms_connection_settings.hpp:54
void setUserAgent(const std::string userAgent)
Set the user agent string.
Definition: ms_connection_settings.cpp:374
void setHttpUsername(const std::string httpUsername)
Sets the username for an authenticated web server.
Definition: ms_connection_settings.cpp:563
std::string getUserAgent() const
Get the user agent string.
Definition: ms_connection_settings.cpp:486
void setHttpPassword(const std::string httpPassword)
Sets the password for an authenticated web server.
Definition: ms_connection_settings.cpp:581
std::string getLastErrorString() const
Return the error description of the last error that occurred.
Definition: ms_errors.cpp:1488
const ms_errs * getErrorHandler() const
Retrive the error object using this function to get access to all errors and error parameters.
Definition: ms_errors.cpp:1518
bool isValid() const
Call this function to determine if there have been any errors.
Definition: ms_errors.cpp:1472
int getErrorRepeats(const int num=-1) const
Returns a number of times the specified error has been repeated.
Definition: ms_errors.cpp:1171
int getErrorNumber(const int num=-1) const
Return a specific error number or ERR_NO_ERROR.
Definition: ms_errors.cpp:1044
std::string getErrorString(const int num) const
Returns a specific error as a string.
Definition: ms_errors.cpp:1094
int getNumberOfErrors() const
Return the number of errors since the last call to clearAllErrors().
Definition: ms_errors.cpp:1004
Indicates any problem that may have occurred during a request to an ms_http_helper object.
Definition: ms_http_helper_return.hpp:37
bool isOk() const
Returns true if no problem was encountered.
Definition: ms_http_helper_return.cpp:109
int getHttpStatusCode() const
Returns the HTTP response code (if the type is ERR_http).
Definition: ms_http_helper_return.cpp:151
std::string getErrorText() const
Returns the text, if any, associated with the error.
Definition: ms_http_helper_return.cpp:161
The class provides HTTP, FTP and FILE protocol access to a server.
Definition: ms_http_helper.hpp:57
ms_http_helper_return httpBufferedOpen(const std::string &action)
Creates and opens a session for buffered reading from the supplied base URL and action.
Definition: ms_http_helper.cpp:2878
ms_http_helper_return httpGetHeader(const std::string &action, std::string &httpHeaderInfo)
Sends an HTTP HEAD message to the server and returns the header information separated with CR LF.
Definition: ms_http_helper.cpp:2525
ms_http_helper_return httpBufferedGetString(std::string &httpBuffer, unsigned long requiredSize)
Get available data from the open session. Writes data into the supplied buffer up to at least the req...
Definition: ms_http_helper.cpp:2934
ms_http_helper_return httpGetString(const std::string &action, std::string &httpGETString)
Sends an HTTP GET message to the server and returns the reply in a string.
Definition: ms_http_helper.cpp:888
ms_http_helper_return httpBufferedClose()
Closes the current buffered read session.
Definition: ms_http_helper.cpp:2684