Faire une requête https en utilisant les sockets sur Linux

Comment puis-je faire une requête http en utilisant des sockets sur Linux? actuellement, je vais

HTTP/1.1 301 Moved Permanently //etc Location: https://server.com 

Voici la partie pertinente du code (la fonction est trop grande pour publier ici):

  /* Socket file descriptor. */ int sock; struct sockaddr_in sockaddr; struct hostent *host; /* Host information. */ sock = socket(AF_INET, /* IPV4 protocol. */ SOCK_STREAM, /* TCP socket. */ 0); /* O for socket() function choose the correct protocol based on the socket type. */ if(sock == INVALID_SOCKET) return SOCK_GENERROR; if((host = gethostbyname(server)) == NULL) { close(sock); return SOCK_HOSTNFOUND; } /* zero buffer */ memset(&sockaddr, 0, sizeof(sockaddr)); sockaddr.sin_family = AF_INET; memcpy(&sockaddr.sin_addr, host -> h_addr, host -> h_length ); sockaddr.sin_port = htons(port); if(connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == INVALID_SOCKET) { close(sock); return SOCK_FERRCONN; } if(send(sock, sendbuf, bufsize, 0) == INVALID_SOCKET) { close(sock); return SOCK_FERRWRITE; } if((readed = recv(sock, recvbuffer, sizeof(recvbuffer), 0)) <= 0) break; 

dans l’appel, server="server.com"; et port=80;

J’ai essayé de supprimer le plus possible mes nouvelles routines et de taper ce code pour vous simplifier la vie.

https requêtes https ressemblent aux requêtes http , mais avec un cryptage transparent de la communication réelle entre le client et le serveur, et sur un autre port par défaut. La bonne nouvelle est que le cryptage transparent vous permet de programmer comme si vous écriviez un client HTTP normal. La mauvaise nouvelle est que le cryptage est suffisamment complexe pour que vous ayez besoin d’une bibliothèque spécialisée pour l’implémenter.

Une telle bibliothèque est OpenSSL . En utilisant OpenSSL, le code minimal pour un client ressemblerait à ceci:

 #include  // first connect to the remote as usual, but use the port 443 instead of 80 // initialize OpenSSL - do this once and stash ssl_ctx in a global var SSL_load_error_ssortingngs (); SSL_library_init (); SSL_CTX *ssl_ctx = SSL_CTX_new (SSLv23_client_method ()); // create an SSL connection and attach it to the socket SSL *conn = SSL_new(ssl_ctx); SSL_set_fd(conn, sock); // perform the SSL/TLS handshake with the server - when on the // server side, this would use SSL_accept() int err = SSL_connect(conn); if (err != 1) abort(); // handle error // now proceed with HTTP traffic, using SSL_read instead of recv() and // SSL_write instead of send(), and SSL_shutdown/SSL_free before close() 

HTTPS est comme HTTP, mais il est encapsulé dans une couche SSL cryptographique. Vous devrez utiliser une lib comme OpenSSL pour créer ces connexions HTTPS.

OpenSSL fournira des fonctions qui remplacent celles de socket.h, pour se connecter, lire et écrire des HTTP normaux (ou tout autre protocole que vous souhaitez utiliser) via un canal SSL, rendant la gestion de la partie SSL transparente pour vous.