curl和openssl交叉编译和curl命令使用
程序员文章站
2022-06-29 14:28:20
...
curl依赖openssl,所以需要先编译openssl:
编译openssl:
下载openssl:https://github.com/openssl/openssl/tree/OpenSSL_1_0_2r,下载解压。
参照:https://blog.csdn.net/ty3219/article/details/77717478 的方法一去编译,分别执行:
./Configure --prefix=/home/nfsshare/hisi/curl/curl/openssl shared no-asm linux-armv4
make CC="arm-none-linux-gnueabi-gcc" AR="arm-none-linux-gnueabi-ar r" RANLIB="arm-none-linux-gnueabi-ranlib" LD="arm-none-linux-ld"
make install
结果出现:Relocations in generic ELF (EM: 40)错误,格式不正确:
/usr/bin/ld: /opt/wifi/tool/openssl/lib/libcrypto.a(ex_data.o): Relocations in generic ELF (EM: 40)
/usr/bin/ld: /opt/wifi/tool/openssl/lib/libcrypto.a(ex_data.o): Relocations in generic ELF (EM: 40)
/opt/wifi/tool/openssl/lib/libcrypto.a: could not read symbols: File in wrong format
collect2: ld 返回 1
make: *** [hostapd] 错误 1
未找到合适和解决方法,于是放弃此方法,改用方法二:
执行config:
make distclean
./config --prefix=/home/nfsshare/hisi/curl/curl/openssl -shared no-asm
然后 vi Makefile 修改 CC=arm-linux-gcc 并将 -m64 参数去掉,然后 make,make install,就安装完成了。
接下来编译curl:
从 https://curl.haxx.se/download/curl-7.67.0.tar.gz 下载源码,执行:
./configure --prefix=/home/nfsshare/hisi/curl/curl --host=arm-none-linux CC=arm-linux-gcc --with-ssl=/home/nfsshare/hisi/curl/curl/openssl
make
make install
于是curl编译就完成了,将编译出来的curl不需要的目录文件(.a、include等)清理掉,拷贝到设备上就可以开始执行了。
curl使用:
首先设置环境变量:
export LD_LIBRARY_PATH=/mnt/curl/lib:/mnt/curl/openssl/lib:$LD_LIBRARY_PATH
发送GET请求比较简单:
./curl http://192.168.1.3/a.asp
发送POST请求:
./curl -d "username=admin&z999=z999&password=admin&Login=&platform=pc&langSelection=zhcn" http://192.168.1.3/goform/login
./curl -d "na" http://192.168.1.3/goform/get_web_hotspots_list
更复杂的:
./curl -H "Content-Type:application/json" -X POST -d '{"user": "admin", "passwd":"admin"}' http://192.168.1.3/login
当然也可以代码控制,参考:
// Write callback.
size_t WriteCallback(char *ptr, size_t size, size_t nitems, void *userdata) {
std::string* str = reinterpret_cast<std::string*>(userdata);
str->append(ptr, size * nitems);
return size * nitems;
}
bool Post(const std::string & url, const std::string & data, std::string & content)
{
// Check parameters.
if (url.empty()) {
assert(false);
return false;
}
content.clear();
// Initialize curl.
CURL* curl = Curl::curl_easy_init();
if (!curl) {
assert(false);
return false;
}
Curl::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
Curl::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
Curl::curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
Curl::curl_easy_setopt(curl, CURLOPT_POST, 1L);
Curl::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
Curl::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
Curl::curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
Curl::curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Curl::curl_easy_setopt (curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
//Curl::curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000L);//超时设置
// Get.
CURLcode status = Curl::curl_easy_perform(curl);
int code = 0;
Curl::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
// Cleanup.
Curl::curl_easy_cleanup(curl);
if (CURLE_OK != status) {
LOG_ERROR("curl_easy_perform failed: " << status);
return false;
}
if (code != 200) {
LOG_INFO("response code: " << code);
return false;
}
return true;
}
注:编译出现You need Perl 5.,则需要安装peral5,参照:
cd perl-5.28.0
./Configure -des -Dprefix=$HOME/localperl
make
make install
注2:对于不是自己的页面,可以用 Wireshark 抓包,找POST,GET等包请求的内容,如:
上一篇: A Simple Nim hdu5795
下一篇: Android Map申请key