Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1549

OpenSSL in C++ Socket Connection (HTTPS Client)

$
0
0

i wrote an script, who can create an connection to an HTTP Server and shows the content of the website in the console. Very easy.

But i want to connect to an https server and do the same procedures. I searched at google and didn't found what i searched.

Please help me and give me an tutorial who can i use the openssl library.

I tried myself on the openssl library, but the library is very complicated and difficult to understand.

Here is my code of the http client:

#include <iostream>#include <ctype.h>#include <cstring>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <netinet/in.h>#include <unistd.h>#include <sstream>#include <fstream>#include <string>#include <arpa/inet.h>#include <openssl/ssl.h>using namespace std;int sock;struct sockaddr_in client;int PORT = 80;int main(int argc, char const *argv[]){    bzero(&client, sizeof(client));    client.sin_family = AF_INET;    client.sin_port = htons( PORT );    client.sin_addr.s_addr = inet_addr("172.16.0.6");    sock = socket(AF_INET, SOCK_STREAM, 0);    if (sock < 0) {        cout << "Error creating socket."<< endl;        exit(1);    }    if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {        close(sock);        cout << "Could not connect"<< endl;        exit(1);    }    stringstream ss;    ss << "GET /"<< "\r\n"<< "Host: 172.16.1.4\r\n"<< "Accept: application/json\r\n"<< "Connection: close"<< "\r\n\r\n";    string request = ss.str();    if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {        cout << "Error sending request."<< endl;        exit(1);    }    char cur;    while ( read(sock, &cur, 1) > 0 ) {        cout << cur;    }    return 0;}

Viewing all articles
Browse latest Browse all 1549

Trending Articles