Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinSock2 HTTP GET usage

int RiotAPI::getSite(std::string hostname) //RiotAPI is my class, almost empty atm
{

if ( wsa ) //wsa is true, dont mind this.
{
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_socktype = SOCK_STREAM;

    errcode = getaddrinfo( hostname.c_str(), HTTPPORT, &hints, &result ); //HTTPPORT defined as "80" <- string
    if ( errcode != 0)
    {
        printf("getaddrinfo() failed error: %d\n", errcode);
    }
    else
        printf("getaddrinfo success\n");

    /*for ( ptr=result; ptr != NULL; ptr->ai_next )
    {
        cSock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
        if ( cSock == INVALID_SOCKET )
        {
            printf("Socket creating failed.\n");
        }
    }*/

    cSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol); //didnt bother looping through results because the web address only returns 1 IP

    errcode = connect(cSock, result->ai_addr, (int)result->ai_addrlen);
    if ( errcode != 0 )
        printf("Could not connect to the server");
    else{
    char request [] = "GET /api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=<MY_API_KEY> HTTP/1.0\r\n";
    send(cSock, request, (int)strlen(request), 0);

    char output [256];
    int bytesrecv = 0;
    //char * cmp = "{\"id\":585897,\"name\":\"RiotSchmick\",\"profileIconId\":583,\"summonerLevel\":30,\"revisionDate\":1387143444000,\"revisionDateStr\":\"12/15/2013 09:37 PM UTC\"}";
    bytesrecv = recv(cSock, output, 255, 0);
    printf("Bytes read: %d\n", bytesrecv);
    printf("Output string: %s\n", output);
    }
}
    return 0;
}

I have been trying to code a program that uses the Riot Games API (League of Legends).

My code should work fine, I tried to GET a webpage from my friends server and it worked, it was index though. (I used "GET HTTP/1.0\r\n")

If I want to get the contents of this url:

http://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=<here_goes_my_api_key>

Shouldn't I proceed like this:

1) Connect to htt://prod.api.pvp.net

2) Send "GET /api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key= HTTP/1.0\r\n"

3) recv() until returns 0, then print out the buffer (I don't do this in my code though, I just tried receiving 255 bytes to see if it worked)

Problem is that it just sits on the recv() function (blocks?). Is something wrong with my GET request?

Here's the API information page: https://i.sstatic.net/pdz58.png

I did try to use the headers in my request but the same thing, recv() just sits there.

I've also tried this:

char buffer [2048];
recv(cSock, buffer, 2047, 0);
printf("Output string: %s\n", buffer); 

This code returns the page fully (not all 2048 bytes are filled though):

While this just returns 3 random jibberish characters.

std::string output = "";
char buffer [128];
//int bytesrecv = 0;

while ( recv(cSock, buffer, 127, 0) > 0)
{
    output += buffer;
}
printf("Output string: %s\n", output); 
like image 974
user2699298 Avatar asked May 06 '26 08:05

user2699298


1 Answers

Your GET request isn't complete, minimally it needs to end with a blank line... i.e. \r\n\r\n at the end. But you're likely to run into other problems, like requiring a Host header if the server is servicing multiple domains...

like image 171
mark Avatar answered May 08 '26 01:05

mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!