I am writing a pure native application for android/ios and am having trouble with the networking code. I can’t seem to get curl to compile, and either way I’d prefer to have a more object oriented library.
What is a good library to do this with or method? My current method to get json from HTTP Requests is this code using SDL2_Net
Code:
class HTTPRequest{
protected:
std::map<std::string, std::string> uris;
std::string url;
public:
std::string host;
HTTPRequest(std::string path) : url(path), host("182.50.154.140") {}
void addURI(std::string parameter, std::string value){
uris[parameter] = value;
}
std::string sendRequest(bool useCookie=false){
std::string temppath = url;
if(!uris.empty()){
temppath += '?';
for(auto &a : uris){
temppath += a.first;
temppath += '=';
temppath += a.second;
temppath += '&';
}
}
IPaddress* addr = new IPaddress;
SDLNet_ResolveHost(addr, host.c_str(), 8080);
TCPsocket sock = SDLNet_TCP_Open(addr);
std::string a = "GET ", b = " HTTP/1.1\r\nHost: 182.50.154.140:8080\r\nConnection: close\r\n\r\n";
std::string c = a+temppath+b;
SDLNet_TCP_Send(sock, c.c_str(), c.length());
std::string d;
char* buf = new char[1024];
while(SDLNet_TCP_Recv(sock, buf, 1024) > 0){
d += buf;
delete[] buf;
buf = new char[1024];
}
delete[] buf;
SDLNet_TCP_Close(sock);
int p = d.find("{");
d.erase(0, p-1);
return d;
}
};
This breaks on large pages though.