64、ubuntu20.04安装Postman测试http通信和测试其libcurl支持http客户端发送request
基本思想:需要使用http协议完成业务需求,需要测试一下,所以学习一下想关的应用实践
一、下载Postman Postman
-
ubuntu@ubuntu:~$ tar-zxvf postman-linux-x64.tar.gz
-
ubuntu@ubuntu:~/postman-linux-x64/Postman$ ./Postman
帐号某宝解决,也可以试用30天
二、固定postman到任务栏图标
ubuntu@ubuntu:~$ sudo gedit /usr/share/applications/Postman.desktop
添加内容
-
[Desktop Entry]
-
Type=Application
-
Name=Postman
-
GenericName=Postman
-
Comment=Postman:The Postman IDE
-
Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
-
Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
-
Terminal=Postman
-
Categories=Postman
再次设置一下图标。使用下列命令打开Postman然后用十字光标点击一下postman工具,显示下列字段
-
ubuntu@ubuntu:~$ xprop |grep WM_CLASS
-
WM_CLASS(STRING) = "postman", "Postman"
-
ubuntu@ubuntu:~$
重新修正配置文件
-
[Desktop Entry]
-
Type=Application
-
Name=Postman
-
GenericName=Postman
-
Comment=Postman:The Postman IDE
-
Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
-
Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
-
Terminal=Postman
-
Categories=Postman
-
StartupWMClass=postman
再次重启Postman就可以添加任务栏,固定住
发送成功,同时参考它的例子,将其代码抄下来,将https改成http,这样不用ssl支持,就可以在代码中集成使用了
四、系统好像默认支持curl,可以不用编译源码安装或者命令安装,跳过源码安装直接执行下面的事例子代码执行和测试
ubuntu@ubuntu:~$ sudo apt-get install curl
或者源码下载libcurl :curl downloads
-
ubuntu@ubuntu:~$ wget https://curl.se/download/curl-7.85.0.tar.gz
-
ubuntu@ubuntu:~$ tar -zxvf curl-7.85.0.tar.gz
-
ubuntu@ubuntu:~/curl-7.85.0$ ./configure --without-ssl
-
ubuntu@ubuntu:~/curl-7.85.0$ make
-
ubuntu@ubuntu:~/curl-7.85.0$ sudo make install
测试代码
cmakelists.txt
-
cmake_minimum_required(VERSION 3.16)
-
project(demo_curl)
-
-
set(CMAKE_CXX_STANDARD 14)
-
-
add_executable(demo_curl main.cpp)
-
target_link_libraries(demo_curl -lcurl )
源码
-
#include<stdio.h>
-
#include<curl/curl.h>
-
int main(int argc, char *argv[])
-
-
{
-
CURL *curl;
-
CURLcode res;
-
curl = curl_easy_init();
-
if(curl) {
-
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
-
curl_easy_setopt(curl, CURLOPT_URL, "192.168.10.26:8334/api/hello");
-
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
-
struct curl_slist *headers = NULL;
-
headers = curl_slist_append(headers, "Authorization: Basic dmNoYXdsYTpIZXJlQDExMTE=");
-
headers = curl_slist_append(headers, "Content-Type: application/json");
-
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
-
const char *data = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
-
res = curl_easy_perform(curl);
-
if(res != CURLE_OK)
-
fprintf(stderr, "curl_easy_perform() failed: %s\n",
-
curl_easy_strerror(res));
-
curl_easy_cleanup(curl);
-
}
-
-
curl_global_cleanup();
-
-
return 0;
-
}
测试结果
-
/home/ubuntu/demo_curk/cmake-build-debug/demo_curl
-
{ "result": welcome to httpserver }
-
Process finished with exit code 0
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhebjjff
系列文章
更多
同类精品
更多
-
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24 -
微信小程序没声音怎么办
PHP中文网 06-15 -
微信提示登录环境异常是什么意思原因
PHP中文网 04-09 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
使用云服务器搭建个人游戏加速器教程
AuroraJay 07-06 -
微信人名旁边有个图标有什么用
PHP中文网 03-11 -
Excel筛选和排序是灰色的怎么办
PHP中文网 06-22 -
抖音国际版要用什么加速器能流畅刷Tiktok的加速器
TK小达人 08-02 -
键盘上的负数是哪个键
PHP中文网 03-17