Richiesta HTTP per GitHub API (C++)

di il
2 risposte

Richiesta HTTP per GitHub API (C++)

Salve a tutti, vorrei scrivere un semplice programma in C++ che sia in grado di mandare una richiesta al server API di GitHub, ma non ottengo ciò che vorrei e non capisco dove sto sbagliando. Per mandare la richiesta ho deciso di usare le socket, essendo su windows (so che esiste una libreria chiamata curlpp ma preferirei agire tramite le socket se possibile).

Il mio problema è il seguente: ho necessità di fare una richiesta alle API di GitHub per conoscere l'ultima release di un progetto, sulla "wiki" di GitHub viene detto che c'è bisogno di impostare la richiesta in questo modo "GET /repos/:owner/:repo/releases/latest" (https://developer.github.com/v3/repos/releases/#get-the-latest-release), e cercando ancora un po' ho trovato che le richieste devono essere fatte all'host di nome "api.github.com" e che il protocollo è https (quindi sulla porta 443).
Il codice per il programma è il seguente:

#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>

using namespace std;


int main( void ){

WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount=0;
int rowCount=0;
struct hostent *host;
locale local;
char buffer[10000];
int i = 0 ;
int nDataLength;
string website_HTML;

// website url
string url = "https://api.github.com";

//HTTP GET
string get_http = "GET /repos/<utente>/<repo>/releases/latest";


    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
        cout << "WSAStartup failed.\n";
        system("pause");
        //return 1;
    }

    Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    host = gethostbyname(url.c_str());

    SockAddr.sin_port=htons(443);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
        cout << "Could not connect";
        system("pause");
        //return 1;
    }

    // send GET / HTTP
    send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );

    // recieve html
    while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){        
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){

            website_HTML+=buffer[i];
            i += 1;
        }               
    }

    closesocket(Socket);
    WSACleanup();

    // Display HTML source 
    cout<<website_HTML;

    // pause
    cout<<"\n\nPress ANY key to close.\n\n";
    cin.ignore(); cin.get(); 


 return 0;
}
Ovviamente ho sostituito <utente> con il nome del proprietario del progetto e <repo> col nome del progetto. In ogni caso quando eseguo il programma non mi da nessun output, semplicemente si termina senza stampare nessun errore, non mi chiede neanche "Press ANY key to close.".

Non so cosa fare, è la prima volta che provo a creare una richiesta http

2 Risposte

Devi accedere o registrarti per scrivere nel forum
2 risposte