Matrix Science header

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;
}
Copyright © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50