HTTP client " Winhttpsend request error"

Sep 17, 2014 at 11:02 PM
This is the code for which I am getting a Winhttpsend request error . Some of the community members have experienced similar issue and have resolved it . Kindly let me know your inputs
    //set credentials
    http_client_config config1;
    printf("3");
    credentials cred = credentials(U("admin"), U("admin"));
    printf("4");
    config1.set_credentials(cred);
    //set method        
    // ## http_client ## Use http_client to create a connection to HTTP service
    // The client constructor either accepts a string or a URI 
    printf("5");
    http_client session(U("https://10.11.1.152:9440"), config1);
    printf("6");
    http_request request;
    printf("7");
    //set method
    request.set_method(methods::GET);
    printf("8");
    //  ## URI ## - Uniform request identifiers that are used for defining entities based on the context . In the case of HTTPS and REST , URI will be used to
    // identify the protocol , the server , port , parameters 
    // URI format --- > protocol : // server [: port] / path ? query # fragment 
    // Example web::uri_builder builder;          
    // Example builder.set_query(U("key1=val1"));
    // Example builder.set_fragment(U("last"));
    // Example uri_builder builder(U("https://10.11.1.152:9440/"));
    // Exampke builder.set_path(U("PrismGateway/services/rest/v1/protection_domains/snapshots/"));
    request.set_request_uri(uri(U("/PrismGateway/services/rest/v1/protection_domains/snapshots/")));
    printf("9");
    //request.set_body("application/json"); - To be used only for PUT
    printf("10");
        // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        printf("10");
        http_response response = session.request(request).get();
        printf("Received response status code:%u\n", response.status_code());
        printf("12");
    }
    catch (std::exception& e)
    {
        printf(e.what());
    } 
    }
Coordinator
Sep 18, 2014 at 4:32 PM
Hi parthapratimnayak,

Try catching http_exception instead of std::exception, here is the reference documentation for it. The http_exception class contains a std::error_code which with have the underlying operating system error and a message about what the cause was.

Steve
Sep 18, 2014 at 11:36 PM
Hi Steve

I did that change and found that the error was due to invalid certificates . I set the validate certificate to FALSE and it has worked .
However , I consistently get the 401 error when I try to invoke the REST API even though the credentials passed by me is correct . This is the code

pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
    *fileStream = outFile;

    //set credentials
    http_client_config config_cred;
    printf("3");
    credentials cred = credentials(U("admin"), U("admin")); // to be parameterized
    printf("4");
    config_cred.set_credentials(cred);
    config_cred.set_validate_certificates(false);
    printf("5");
    // ## http_client ## Use http_client to create a connection to HTTP service
    // The client constructor either accepts a string or a URI 
    http_client client(U("https://10.11.1.152:9440/"), config_cred);// to be parameterized
    printf("6");
    //  ## URI ## - Uniform request identifiers that are used for defining entities based on the context . In the case of HTTPS and REST , URI will be used to
    // identify the protocol , the server , port , parameters 
    // URI format --- > protocol : // server [: port] / path ? query # fragment
    //Example --builder.set_query(U("key1=val1"));
    //builder.set_fragment(U("last"));
    uri_builder builder;
    printf("7");
    builder.set_path(U("PrismGateway/services/rest/v1/protection_domains/snapshots/")); // to be parameterized
    printf("8");
    return client.request(methods::GET, builder.to_string());
    printf("9");
})
    // Handle response headers arriving.
    // task<http_response>will provide the response object once the headers have arrived. 
    // To get the http_response object and avoid blocking the current thread and use .then() :
    .then([=](http_response response)
{
    printf("Received response status code:%u\n", response.status_code());
    printf("Received response :%u\n", response.body());
    // ## streambuf ## Write response body into the file by initializing output stream using streambuf()
    // ## fstream ## Input stream class to operate on files 
    return response.body().read_to_end(fileStream->streambuf());
})
Coordinator
Sep 19, 2014 at 11:10 PM
Hi parthapratimnayak,

I would be very careful about disabling certificate verification, you should probably only do this for experimenting or testing at most.

What platform are you running on? Is it a non Windows one? If so we haven't implemented authentication yet, here is the feature request. If you are using something like Basic authentication you can easily do it yourself by adding the headers.

Steve