我正在为 android/ios 编写一个纯 native 应用程序,但在网络代码方面遇到了问题。我似乎无法让 curl 进行编译,无论哪种方式,我都希望有一个更面向对象的库。
用什么方法做这个的好库是什么?我目前从 HTTP 请求中获取 json 的方法是使用 SDL2_Net 的代码
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;
}
};
不过这会在大页面上中断。
最佳答案
cURL 是您的最佳选择。这里有几个链接,您可以尝试移植 cURL
http://thesoftwarerogue.blogspot.com/2010/05/porting-of-libcurl-to-android-os-using.html https://github.com/jahrome/curl-android
如果您仍然无法使用 cURL 成功,请尝试 POCO
关于c++ - 如何使用 Android ndk 进行 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677711/