3
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.stack.imgur.com/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); 
user2943407
  • 409
  • 3
  • 5
  • 13
user2699298
  • 1,446
  • 3
  • 17
  • 33

1 Answers1

1

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...

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
mark
  • 5,269
  • 2
  • 21
  • 34
  • Yes, I did fix the \r\n\r\n at the end... then I have those 2 problems below the code. What do you mean by the second part? – user2699298 Dec 16 '13 at 21:07
  • Let's say a web server is hosting several different domains, if your GET request asks for the default page at / then the only way the web server knows which domain you're referring to is via the Host header. Many servers flat out require it even though it's a HTTP/1.1 header. The recv blocking is indicative of the server waiting for you to complete a request... – mark Dec 16 '13 at 21:09
  • But they would specify that in the API information, wouldn't they? I've added an image of the API documentation in the first post. – user2699298 Dec 16 '13 at 21:12
  • I also note that they are specifying HTTPS (encrypted) connection. Perhaps they are ignoring your unencrypted attempt. – mark Dec 16 '13 at 21:20
  • So, what am I supposed to now? How do I make a test and see what exactly happens? – user2699298 Dec 16 '13 at 21:23
  • Well, you've got lots of options. If you're on windows you might look into the WinHttp API, or you could go to a lower level and look at Secure Channel. Or if you're familiar with OpenSSL that can certainly handle encryption for you on several different platforms. Or just grab a command line utility like wget and try it out. – mark Dec 16 '13 at 21:54