httprequest with curl in C returning values

0

Hello everyone reading.

I'm trying to create a simple md5 hash file validation code with my web server.

I basically want to send the hash and if it is the same registered on my web server and it will return with true or false, if true returns the value 5 and false.

Here is my code below:

#include <windows.h>
#include <stdio.h>
#include <string>
#include <tchar.h>
#include <cstdio>
#include <atlbase.h>
#include <msxml6.h>
#include <comutil.h>
#include <iostream>
#pragma comment(lib,"msxml6")
#pragma comment(lib,"comsuppwd")

using namespace std;
void HTTPReq(
    const char* verb,
    const char* hostname,
    int port,
    const char* resource,
    const char* opt_urlencoded,
    string& response)
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    {
        cout << "WSAStartup failed.\n";
        exit(1);
    }

    SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    struct hostent *host;
    host = gethostbyname(hostname);

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

    if (connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0)
    {
        exit(1);
    }

    // Build request
    string req = verb; // GET | POST
    req.append(" ");
    // Note, on GET, 'resource' must contain the encoded parameters, if any:
    req.append(resource);
    req.append(" HTTP/1.1\r\n");

    req.append("Host: ");
    req.append(hostname);
    req.append(":");
    req.append(to_string(static_cast<long long>(port)));
    req.append("\r\n");

    if (strcmp(verb, "POST") == 0)
    {
        req.append("Cache-Control: no-cache\r\n");
        req.append("Content-length: ");
        req.append(to_string(static_cast<long long>(strlen(opt_urlencoded))));
        req.append("\r\n");
        req.append("Content-Type: application/x-www-form-urlencoded\r\n\r\n");

        // User is required to handle URI encoding for this value
        req.append(opt_urlencoded);

    }
    else if (strcmp(verb, "GET") == 0)
    {
        req.append("Cache-Control: no-cache\r\n");
        req.append("Connection: close\r\n\r\n");
    }


    send(Socket, req.c_str(), req.size(), 0);

    char buffer[1024*10];
    int nlen;

    while ((nlen = recv(Socket,buffer,1024*10,0)) > 0)
    {
        response.append(buffer, 0, nlen);
    }
    closesocket(Socket);
    WSACleanup();

}


int CurlURL(char *CG)
{
    std::string response;
    char time[15];

    char url[500] = "index.php?cg=";

    strcat(url,CG);

    HTTPReq("GET", "site.com", 80, url, NULL, response);
//  response.copy(time, 15,220);
//  time[15] = '
#include <windows.h>
#include <stdio.h>
#include <string>
#include <tchar.h>
#include <cstdio>
#include <atlbase.h>
#include <msxml6.h>
#include <comutil.h>
#include <iostream>
#pragma comment(lib,"msxml6")
#pragma comment(lib,"comsuppwd")

using namespace std;
void HTTPReq(
    const char* verb,
    const char* hostname,
    int port,
    const char* resource,
    const char* opt_urlencoded,
    string& response)
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    {
        cout << "WSAStartup failed.\n";
        exit(1);
    }

    SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    struct hostent *host;
    host = gethostbyname(hostname);

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

    if (connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0)
    {
        exit(1);
    }

    // Build request
    string req = verb; // GET | POST
    req.append(" ");
    // Note, on GET, 'resource' must contain the encoded parameters, if any:
    req.append(resource);
    req.append(" HTTP/1.1\r\n");

    req.append("Host: ");
    req.append(hostname);
    req.append(":");
    req.append(to_string(static_cast<long long>(port)));
    req.append("\r\n");

    if (strcmp(verb, "POST") == 0)
    {
        req.append("Cache-Control: no-cache\r\n");
        req.append("Content-length: ");
        req.append(to_string(static_cast<long long>(strlen(opt_urlencoded))));
        req.append("\r\n");
        req.append("Content-Type: application/x-www-form-urlencoded\r\n\r\n");

        // User is required to handle URI encoding for this value
        req.append(opt_urlencoded);

    }
    else if (strcmp(verb, "GET") == 0)
    {
        req.append("Cache-Control: no-cache\r\n");
        req.append("Connection: close\r\n\r\n");
    }


    send(Socket, req.c_str(), req.size(), 0);

    char buffer[1024*10];
    int nlen;

    while ((nlen = recv(Socket,buffer,1024*10,0)) > 0)
    {
        response.append(buffer, 0, nlen);
    }
    closesocket(Socket);
    WSACleanup();

}


int CurlURL(char *CG)
{
    std::string response;
    char time[15];

    char url[500] = "index.php?cg=";

    strcat(url,CG);

    HTTPReq("GET", "site.com", 80, url, NULL, response);
//  response.copy(time, 15,220);
//  time[15] = '%pre%';
//  std::cout << time;

    if ( strstr(response.c_str(),"true") ){ //autoriza
    return 5; // valor a ser retornado se for true
    } else if ( strstr(response.c_str(),"false") ){ // rejeita
    return 1; // valor a ser retornado se for false
    }
}
'; // std::cout << time; if ( strstr(response.c_str(),"true") ){ //autoriza return 5; // valor a ser retornado se for true } else if ( strstr(response.c_str(),"false") ){ // rejeita return 1; // valor a ser retornado se for false } }
    
asked by anonymous 17.04.2018 / 18:01

0 answers