1

I'm working on an application and I'm trying to "license" it by sending an API key with application versioning to my website and get the return value of the webpage.

User opens application --> Application sends a GET request with 2 values to the webserver: ?authkey=[key]&v=1.00 --> grab the return value

I have tried to work on this but it always seems to fail on me. This is where I'm currently at:

char buffer[1024] =
    "GET / HTTP/1.1\r\n"
    "Host: example.com\r\n"
    "Accept-Language: en-US,en;q=0.5\r\n"
    "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n"
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    "Connection: keep-alive\r\n"
    "Cache-Control: max-age=0\r\n\r\n";

size_t buffer_len = sizeof(buffer) - 1;

n = write(sockfd, buffer, buffer_len);

bzero(buffer, strlen(buffer));
n = read(sockfd, buffer, buffer_len);

printf("%s\n",buffer);

sysNotify(buffer);

// if(buffer == "valid") // continue?

But I have no idea how to send the GET request, and how to compare the return value afterwards.

If anybody would like to assist me with this, that would be amazingly appreciated.

J. Doe
  • 143
  • 1
  • 10

1 Answers1

0

First, you never specify your query string in your GET request, so your buffer should look more like:

char buffer[1024];
char query_string[1024];
...
sprintf(query_string, "?authkey=%s&v=1.00", key);
... 
sprintf(buffer,
    "GET /%s HTTP/1.1\r\n"
    "Host: example.com\r\n"
    "Accept-Language: en-US,en;q=0.5\r\n"
    "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n"
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    "Connection: keep-alive\r\n"
    "Cache-Control: max-age=0\r\n\r\n",
    query_string);

Secondly, you don't seem to connect before write on a TCP connection (or maybe you do but didn't post that code); before your write you should do:

if (connect(sockfd, (struct sockaddr*)&sockaddr_in, sizeof(sockaddr_in)) == -1) {
        exit(EXIT_FAILURE);
}

There are a few working implementations of GET in C you can look up on SO, like this one.

mnistic
  • 10,866
  • 2
  • 19
  • 33