2013-04-27 16 views
5

Come si effettua una richiesta http utilizzando i socket su linux? Al momento, mi stoEffettuare una richiesta https utilizzando i socket su linux

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

qui è parte rilevante del codice (la funzione è troppo grande per postare qui):

/* 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; 

nella chiamata, server="server.com"; e port=80;

ho cercato di rimuovere il più possibile le mie routine onw e digitare da questo codice per rendere più pulito per te.

+0

Stai tentando di effettuare una richiesta HTTP o HTTPS? Quali dati stai inviando in 'sendbuf'? – Julien

+0

@ Giuli: una richiesta https. – Jack

risposta

10

https richieste come le richieste http, ma con crittografia trasparente della comunicazione effettiva tra il client e il server e su una porta predefinita diversa. La buona notizia è che la crittografia trasparente ti permette di programmare esattamente come stai scrivendo un normale client HTTP. La cattiva notizia è che la crittografia è abbastanza complessa da richiedere una libreria specializzata per implementarla per te.

Una tale libreria è OpenSSL. Usando OpenSSL, il codice minimo per un client sarebbe il seguente:

#include <openssl/ssl.h> 

// 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_strings(); 
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() 
+1

Grazie! ha funzionato bene – Jack

+0

Dove posso trovare della documentazione per questa libreria? – Jack

+2

@Jack Le pagine man delle funzioni della libreria sono disponibili online. Inoltre, la libreria è molto utilizzata, quindi puoi trovare esempi su Google e ne troverai parecchi. – user4815162342

3

HTTPS è proprio come HTTP, ma è incapsulato in un livello SSL crittografico. Avrai bisogno di usare una lib come OpenSSL per rendere quelle connessioni HTTPS.

OpenSSL fornirà funzioni che sostituiscono i socket.h, per connettere, leggere e scrivere regolarmente HTTP (o qualsiasi altro protocollo che si desidera utilizzare) attraverso un canale SSL, rendendo la gestione della parte SSL trasparente per voi.

+0

Grazie anche a te! :) – Jack

Problemi correlati