C语言CURL实现HTTP POST、GET、PUT
- * GET - 从指定的资源请求数据。
- * POST - 向指定的资源提交要被处理的数据
- * PUT请求:如果两个请求相同,后一个请求会把第一个请求覆盖掉。(所以PUT用来改资源)
- * Post请求:后一个请求不会把第一个请求覆盖掉。(所以Post用来增资源)
POST
/**************************************************************************** _ _ ____ _* Project ___| | | | _ \| |* / __| | | | |_) | |* | (__| |_| | _ <| |___* \___|\___/|_| \_\_____|** Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.** This software is licensed as described in the file COPYING, which* you should have received as part of this distribution. The terms* are also available at https://curl.haxx.se/docs/copyright.html.** You may opt to use, copy, modify, merge, publish, distribute and/or sell* copies of the Software, and permit persons to whom the Software is* furnished to do so, under the terms of the COPYING file.** This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY* KIND, either express or implied.****************************************************************************/
/* <DESC>* simple HTTP POST using the easy interface* </DESC>* GET - 从指定的资源请求数据。* POST - 向指定的资源提交要被处理的数据*/
#include <stdio.h>
#include <curl/curl.h>int main(void)
{CURL *curl;CURLcode res;/* In windows, this will init the winsock stuff */curl_global_init(CURL_GLOBAL_ALL);/* get a curl handle */curl = curl_easy_init();if(curl) {/* First set the URL that is about to receive our POST. This URL canjust as well be a https:// URL if that is what should receive thedata. */curl_easy_setopt(curl, CURLOPT_URL, "http://10.170.6.66/moo.cgi");/* Now specify the POST data */curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");/* Perform the request, res will get the return code */res = curl_easy_perform(curl);/* Check for errors */if(res != CURLE_OK)fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));/* always cleanup */curl_easy_cleanup(curl);}curl_global_cleanup();return 0;
}
PUT
/**************************************************************************** _ _ ____ _* Project ___| | | | _ \| |* / __| | | | |_) | |* | (__| |_| | _ <| |___* \___|\___/|_| \_\_____|** Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.** This software is licensed as described in the file COPYING, which* you should have received as part of this distribution. The terms* are also available at https://curl.haxx.se/docs/copyright.html.** You may opt to use, copy, modify, merge, publish, distribute and/or sell* copies of the Software, and permit persons to whom the Software is* furnished to do so, under the terms of the COPYING file.** This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY* KIND, either express or implied.****************************************************************************/
/* <DESC>* HTTP PUT with easy interface and read callback* </DESC>* PUT请求:如果两个请求相同,后一个请求会把第一个请求覆盖掉。(所以PUT用来改资源)* Post请求:后一个请求不会把第一个请求覆盖掉。(所以Post用来增资源)*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <curl/curl.h>/** This example shows a HTTP PUT operation. PUTs a file given as a command* line argument to the URL also given on the command line.** This example also uses its own read callback.** Here's an article on how to setup a PUT handler for Apache:* http://www.apacheweek.com/features/put*/static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{size_t retcode;curl_off_t nread;/* in real-world cases, this would probably get this data differentlyas this fread() stuff is exactly what the library already would doby default internally */retcode = fread(ptr, size, nmemb, stream);nread = (curl_off_t)retcode;fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T" bytes from file\n", nread);return retcode;
}int main(int argc, char **argv)
{CURL *curl;CURLcode res;FILE * hd_src;struct stat file_info;char *file;char *url;if(argc < 3)return 1;file = argv[1];url = argv[2];/* get the file size of the local file */stat(file, &file_info);/* get a FILE * of the same file, could also be made withfdopen() from the previous descriptor, but hey this is justan example! */hd_src = fopen(file, "rb");/* In windows, this will init the winsock stuff */curl_global_init(CURL_GLOBAL_ALL);/* get a curl handle */curl = curl_easy_init();if(curl) {/* we want to use our own read function */curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);/* enable uploading */curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);/* HTTP PUT please */curl_easy_setopt(curl, CURLOPT_PUT, 1L);/* specify target URL, and note that this URL should include a filename, not only a directory */curl_easy_setopt(curl, CURLOPT_URL, url);/* now specify which file to upload */curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);/* provide the size of the upload, we specicially typecast the valueto curl_off_t since we must be sure to use the correct data size */curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);/* Now run off and do what you've been told! */res = curl_easy_perform(curl);/* Check for errors */if(res != CURLE_OK)fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));/* always cleanup */curl_easy_cleanup(curl);}fclose(hd_src); /* close the local file */curl_global_cleanup();return 0;
}
C语言CURL实现HTTP POST、GET、PUT相关推荐
- C语言curl实现FTP上传、下载、获取文件信息
目录 Get a single file from an FTP server. Checks a single file's size and mtime from an FTP server. G ...
- cheat.sh 在手,天下我有
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 前言 作为程序员需要了解的东西有很多,日常编码和写脚本脱 ...
- bigdecimal 平均数_云函数 · – Bmob后端云
Java云函数¶ 云函数是一段部署在服务端的代码片段,采用 java 或 node.js 进行编写,然后部署运行在Bmob服务器 通过云函数可以解决很多复杂的业务逻辑,从此无需将要将大量的数据发送到移 ...
- 【小沐学Python】Python实现在线英语翻译功能
文章目录 1.简介 2.在线翻译接口 2.1 Google Translate API 2.2 Microsoft Translator API 2.2.1 开发简介 2.2.2 开发费用 2.2.3 ...
- curle(curley)
Queen Elizabeth is afraid of Mary. Bess Curle wrote the story. 这两句什么意思? 伊丽莎白女王害怕玛丽,Bess Curle写(说)出了这 ...
- 老农的计算机笔记(三)软件篇
计算机软件 自从冯-诺依曼为现代计算机制定了基本的体系结构--将可执行指令和数据一起放到存储器中,由计算机自动执行,计算机就有了软件. 我们可以把软件理解为像厨师的菜谱一样的东西,计算机按照这个单子去 ...
- ElasticSearch从入门到精通,史上最全(持续更新,未完待续,每天一点点)
目录 1.ElasticSearch的简介 2.用数据库实现搜素的功能 3.ES的核心概念 3.1 NRT(Near Realtime)近实时 3.2 cluster集群,ES是一个分布式的系统 3. ...
- frisby.js-接口测试基础知识
一.安装环境 1.先更新yum语言curl -sL https://rpm.nodesource.com/setup | bash - 2.安装nodejsyum install -y nodejs3 ...
- 学了C语言,如何利用CURL写一个下载程序?—用nmake编译CURL并安装
在这一系列的前一篇文章学了C语言,如何为下载狂人写一个磁盘剩余容量监控程序?中,我们为下载狂人写了一个程序来监视磁盘的剩余容量,防止下载的东西撑爆了硬盘.可是,这两天,他又抱怨他的下载程序不好用,让我 ...
最新文章
- uitableView 选择跳过后, 跳回 颜色变化 问题
- 深层学习:心智如何超越经验2.4 前景
- ONENET读取与控制麒麟座MINI开发板LED状态
- 谷歌chromeos_谷歌浏览器 79.0.3945.79 正式版
- 英特尔将开源进行到底!
- android bootstrap功能,整合QMUI Android和Android-Bootstrap
- 10.企业安全建设入门(基于开源软件打造企业网络安全) --- 数据库安全
- 【代码笔记】iOS-手机系统版本
- 并联串联混合的电压和电流_电子电路基础,教你看懂电子电路,简单的串并联...
- p2p-如何拯救k8s镜像分发的阿喀琉斯之踵
- 图片如何在线生成GIF动画?轻松三步在线制作GIF动画
- 双卡双待手机在使用移动卡的流量上网时为何另外一张联通卡没有信号?
- compare用法java,Java经典用法总结
- 接入层交换机、分布层交换机、核心层交换机
- matlab tcpip数据解析,Matlab数据输出、从MATLAB 以tcpip形式传输数据到 vvvv
- SQL Server链接服务器的使用
- 别把自己变成了“二等公民”
- 佐藤可士和的超整理术
- 【Web】html+js 制作小游戏
- 资源 | 吴恩达斯坦福CS230深度学习课程全套资料放出(附下载)
热门文章
- Idea+springcloud+zookeeper做的服务发现和远程连接zookeeper
- 【JAVA】Maven profiles控制多环境数据源日志打包(转载)
- [转帖]升级 Ubuntu,解决登录时提示有软件包可以更新的问题
- [第二届构建之法论坛] 预培训文档(C++版)
- unrecognized selector sent to instance问题的解决
- eclipse查看android源码包(eclipse导入android源码包)
- 基于CentOs的Hadoop集群全分布式部署
- linux上derby数据库,体验纯Java数据库——Derby
- linux mp4v2编译,Android 编译mp4 v2 2.0.0生成动态库
- java和线程相关的关键字有哪些_Java中有哪些机制来保证线程安全?synchronized关键字和volatile关键字...