THREAD: [C++/SPRX] Get char* from website / webRequests
  1. 12-27-2017, 05:42 AM
    makeabce's Avatar
    makeabce
    I defeated!
    makeabce's Avatar
    makeabce
    I defeated!
    614
    Posts
    5,862
    Reputation
    Feb 2013
    NextGenUpdate
    Hey, i' think i've already asked this once but never got enough info about this...

    So, basicly i want to be able to get char* from websites(on this case my own) to get show up in SPRX Message dialog/in-game messges.
    I tried BLB's webRequest, it did exactly what i needed, but it only worked for MW3 properly. Tried also MW2, but after calling the function,it doesn't let me play online at all,black ops 1/2 this just freezes the console.

    I also tried other code(Some kind of sockets..), it did the job on all games, but the problem was that it showed more than the char* i wanted from the website(HTTP: 1.0, version...ect)
    Tried making function that removes those unnecessary information but it causes freeze or didn't work at all.
  2. 12-27-2017, 06:05 AM
    John's Avatar
    John
    skid
    John's Avatar
    John
    skid
    1,822
    Posts
    65,774
    Reputation
    Dec 2012
    NextGenUpdate

    bool Requests::get(char *host, char *params, char *out, size_t outSz) {
    sockaddr_in SocketAddress;
    hostent *hent = gethostbyname(host);

    SocketAddress.sin_addr.s_addr = *((unsigned long*)hent->h_addr);
    SocketAddress.sin_family = AF_INET;
    SocketAddress.sin_port = htons(80);
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(sock, (struct sockaddr *)&SocketAddress, sizeof(SocketAddress)) != 0) {
    return false;
    }
    char rqBuffer[255] = { 0 };
    strcpy(rqBuffer, "GET /");
    strcat(rqBuffer, (const char *)params);
    strcat(rqBuffer, " HTTP/1.0
    HOST: ");
    strcat(rqBuffer, host);
    strcat(rqBuffer, "

    ");

    send(sock, rqBuffer, 255, 0);

    recv(sock, out, outSz, 0);

    socketclose(sock);

    return true;
    }
    char *Requests::removeRequestHeader(char *request) {
    char *httpbody = strstr(request, "

    ");
    if (httpbody) httpbody += 4;

    return httpbody;
    }


    usage:

    char reqRt[2048];
    char request[255];
    Requests::get("myserver.com", "Version.txt", reqRt, sizeof(reqRt));
    char *reqNoHeader = Requests::removeRequestHeader(reqRt);

    ShowDialog(reqNoHeader);



    potential buffer overflow here, so dont fuck that up
    Last edited by John ; 12-29-2017 at 08:53 PM.

  3. The Following 3 Users Say Thank You to John For This Useful Post:

    , ,

  4. 12-27-2017, 06:51 AM
    makeabce's Avatar
    makeabce
    I defeated!
    makeabce's Avatar
    makeabce
    I defeated!
    614
    Posts
    5,862
    Reputation
    Feb 2013
    NextGenUpdate
    Originally Posted by John View Post

    bool Requests::get(char *host, char *params, char *out, size_t outSz) {
    sockaddr_in SocketAddress;
    hostent *hent = gethostbyname(host);

    SocketAddress.sin_addr.s_addr = *((unsigned long*)hent->h_addr);
    SocketAddress.sin_family = AF_INET;
    SocketAddress.sin_port = htons(80);
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(sock, (struct sockaddr *)&SocketAddress, sizeof(SocketAddress)) != 0) {
    return false;
    }
    char rqBuffer[255] = { 0 };
    strcpy(rqBuffer, "GET /");
    strcat(rqBuffer, (const char *)params);
    strcat(rqBuffer, " HTTP/1.0
    HOST: ");
    strcat(rqBuffer, host);
    strcat(rqBuffer, "

    ");

    send(sock, rqBuffer, 255, 0);

    recv(sock, out, outSz, 0);

    socketclose(sock);

    return true;
    }
    char *Requests::removeRequestHeader(char *request) {
    char *httpbody = strstr(request, "

    ");
    if (httpbody) httpbody += 4;

    return httpbody;
    }


    usage:

    char reqRt[2048];
    char request[255];
    Requests::get("myserver.com", "Version.txt", reqRt, sizeof(reqRt));
    char *reqNoHeader = Requests::removeRequestHeader(reqRt);

    ShowDialog(reqNoHeader);



    Hail to the king! PBJT
  5. 08-29-2018, 09:40 AM
    20
    Posts
    611
    Reputation
    Sep 2014
    NextGenUpdate
    I know this is very old but for me the console freezes every time at this breakpoint
    SocketAddress.sin_addr.s_addr = *((unsigned long*)hent->h_addr);

    If someone read this, I would be greatful for some help.