Post: [C++/SPRX] Get char* from website / webRequests
12-27-2017, 05:42 AM #1
makeabce
I defeated!
(adsbygoogle = window.adsbygoogle || []).push({}); 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.
12-27-2017, 06:05 AM #2
    
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.

The following 3 users say thank you to John for this useful post:

makeabce, Norway-_-1999, ProfoundModz
12-27-2017, 06:51 AM #3
makeabce
I defeated!
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
08-29-2018, 09:40 AM #4
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.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo