• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

64、ubuntu20.04安装Postman测试http通信和测试其libcurl支持http客户端发送request

武飞扬头像
sxj731533730
帮助5

基本思想:需要使用http协议完成业务需求,需要测试一下,所以学习一下想关的应用实践

一、下载Postman Postman

  1.  
    ubuntu@ubuntu:~$ tar-zxvf postman-linux-x64.tar.gz
  2.  
    ubuntu@ubuntu:~/postman-linux-x64/Postman$ ./Postman

帐号某宝解决,也可以试用30天

学新通

二、固定postman到任务栏图标

ubuntu@ubuntu:~$ sudo gedit /usr/share/applications/Postman.desktop

添加内容

  1.  
    [Desktop Entry]
  2.  
    Type=Application
  3.  
    Name=Postman
  4.  
    GenericName=Postman
  5.  
    Comment=Postman:The Postman IDE
  6.  
    Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
  7.  
    Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
  8.  
    Terminal=Postman
  9.  
    Categories=Postman

 再次设置一下图标。使用下列命令打开Postman然后用十字光标点击一下postman工具,显示下列字段

  1.  
    ubuntu@ubuntu:~$ xprop |grep WM_CLASS
  2.  
    WM_CLASS(STRING) = "postman", "Postman"
  3.  
    ubuntu@ubuntu:~$

重新修正配置文件

  1.  
    [Desktop Entry]
  2.  
    Type=Application
  3.  
    Name=Postman
  4.  
    GenericName=Postman
  5.  
    Comment=Postman:The Postman IDE
  6.  
    Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
  7.  
    Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
  8.  
    Terminal=Postman
  9.  
    Categories=Postman
  10.  
    StartupWMClass=postman

再次重启Postman就可以添加任务栏,固定住

发送成功,同时参考它的例子,将其代码抄下来,将https改成http,这样不用ssl支持,就可以在代码中集成使用了

学新通

四、系统好像默认支持curl,可以不用编译源码安装或者命令安装,跳过源码安装直接执行下面的事例子代码执行和测试

ubuntu@ubuntu:~$ sudo apt-get install curl

或者源码下载libcurl :curl downloads

  1.  
    ubuntu@ubuntu:~$ wget https://curl.se/download/curl-7.85.0.tar.gz
  2.  
    ubuntu@ubuntu:~$ tar -zxvf curl-7.85.0.tar.gz
  3.  
    ubuntu@ubuntu:~/curl-7.85.0$ ./configure --without-ssl
  4.  
    ubuntu@ubuntu:~/curl-7.85.0$ make
  5.  
    ubuntu@ubuntu:~/curl-7.85.0$ sudo make install

测试代码

cmakelists.txt

  1.  
    cmake_minimum_required(VERSION 3.16)
  2.  
    project(demo_curl)
  3.  
     
  4.  
    set(CMAKE_CXX_STANDARD 14)
  5.  
     
  6.  
    add_executable(demo_curl main.cpp)
  7.  
    target_link_libraries(demo_curl -lcurl )

源码

  1.  
    #include<stdio.h>
  2.  
    #include<curl/curl.h>
  3.  
    int main(int argc, char *argv[])
  4.  
     
  5.  
    {
  6.  
    CURL *curl;
  7.  
    CURLcode res;
  8.  
    curl = curl_easy_init();
  9.  
    if(curl) {
  10.  
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  11.  
    curl_easy_setopt(curl, CURLOPT_URL, "192.168.10.26:8334/api/hello");
  12.  
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  13.  
    curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
  14.  
    struct curl_slist *headers = NULL;
  15.  
    headers = curl_slist_append(headers, "Authorization: Basic dmNoYXdsYTpIZXJlQDExMTE=");
  16.  
    headers = curl_slist_append(headers, "Content-Type: application/json");
  17.  
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  18.  
    const char *data = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
  19.  
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  20.  
    res = curl_easy_perform(curl);
  21.  
    if(res != CURLE_OK)
  22.  
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
  23.  
    curl_easy_strerror(res));
  24.  
    curl_easy_cleanup(curl);
  25.  
    }
  26.  
     
  27.  
    curl_global_cleanup();
  28.  
     
  29.  
    return 0;
  30.  
    }
学新通

测试结果

  1.  
    /home/ubuntu/demo_curk/cmake-build-debug/demo_curl
  2.  
    { "result": welcome to httpserver }
  3.  
    Process finished with exit code 0

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhebjjff
系列文章
更多 icon
同类精品
更多 icon
继续加载