httprequest with Curl in C returning values

Asked

Viewed 26 times

0

Hello to all who are reading.

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

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

Follow 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] = '\0';
//  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
    }
}
  • and what is the question?

  • I’m sorry if I didn’t, but I explained the problem, but my question is what’s wrong? because it’s not returning the requested true or false value

  • 1

    you debugged your code?

  • shows this: Warning 2 Warning C4715: 'Curlurl' : not all control paths Return a value

  • the strange thing is that when I put in my web server only true or false, but without requiring requirement it le, but if I ask to be ordered will not

  • 1

    Dude, I recommend you really debug your code, what you commented on up there is a Warning..

  • 1

    This Warning is because you have if and else if with a return in each. For the compiler it is not evident that if not for one goes to another. If you want to make this clear change the else if for else removing the condition he had in this else if. The problem will however be related to other things, and should start by debugging as Bertolazo indicated to get a sense of what is working properly in your code

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.