如何使用Android ndk进行HTTP请求
How to do HTTP Requests with Android ndk
我正在为android/ios编写一个纯本机应用程序,但网络代码有问题。我似乎无法编译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.1rnHost: 182.50.154.140:8080rnConnection: closernrn";
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.htmlhttps://github.com/jahrome/curl-android
如果你仍然没有成功使用cURL,尝试POCO
http://pocoproject.org/
相关文章:
- 如何在boost beast http请求中设置http头
- 在多个核心中处理一个HTTP请求
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 使用 winsock 接收 http 请求
- 对于 http 请求,python 比 c++ 快吗?
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 带有C++的 HTTP 请求
- 如何在QT中同步发送http请求
- 我可以使用 Boost.Asio 和 Boost.Beast 库发出 HTTPS 请求或 HTTP/2 请求吗?
- HTTP 请求中的标头名称无效
- 套接字或 HTTP 请求
- Winsock2.h 无法发送 http 请求
- 用libcurl生成http请求作为字符串
- http请求之前和之后的垃圾
- 从主机向在VirtualBox linux机器上运行的服务器发送http请求
- 如何在不阻塞 UI 线程的情况下对C++发出 http 请求
- C++ Boost 1.66 使用 Beast http 请求解析器解析字符串
- 在新线程C++中发送 http 请求
- 有什么方法可以通过按下按钮将HTTP请求从JS发送到C
- 套接字"Resource temporarily unavailable"的原始 HTTP 请求