基于libcurl的GET与POST(HTTP1.1)
#include <stdio.h>
#include <curl/curl.h> bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回结果用文件存储
return false;
struct curl_slist *headers = NULL;
// headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
curl_easy_setopt(curl, CURLOPT_URL,"http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC");
// curl_easy_setopt(curl, CURLOPT_URL,"http://localhost/");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的html主体数据输出到fp指向的文件
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
res = curl_easy_perform(curl); // 执行
if (res != ) { curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp; struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL; if ((fp = fopen(filename, "w")) == NULL)
return false;
curl = curl_easy_init();
if (curl)
{
// curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt"); // 指定cookie文件 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "image",CURLFORM_COPYCONTENTS,"1.jpg",CURLFORM_END);
// curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost); // POST -liuzhenbo
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post内容
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
curl_easy_setopt(curl, CURLOPT_URL, "http://www.nengyouyun.cn/user/getAppversionnew2?apptype=H5C899DDC"); // 指定url
curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost); // POST -liuzhenbo
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //HTTP主体数据 -liuzhenbo
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的http头输出到fp指向的文件
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
}
fclose(fp);
return true;
}
int main(void)
{
getUrl("get");
postUrl("post");
}
GET方式接收到服务器端发来的http头:
HTTP/1.1
Server: nginx/1.10. (Ubuntu)
Date: Mon, Jun :: GMT
Content-Type: application/json;charset=UTF-
Transfer-Encoding: chunked
Connection: keep-alive
X-Application-Context: cloud-gateway:
POST方式接收到服务器发来的http头:
HTTP/1.1 Continue HTTP/1.1
Server: nginx/1.10. (Ubuntu)
Date: Mon, Jun :: GMT
Content-Type: application/json;charset=UTF-
Transfer-Encoding: chunked
Connection: keep-alive
X-Application-Context: cloud-gateway:
注:使用HTTP/1.1协议的curl,当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步。(我认为主要是为了节省资源)
流程如下:
(1)发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据
(2)接收到Server返回的100-continue应答以后, 把数据POST给Server
基于libcurl的GET与POST(HTTP1.1)的更多相关文章
- 基于libcurl实现REST风格http/https的get和post
c/c++开发中经常要用到http/https协议,直接使用socket工作量很大,要是使用socket实现https,那更不可思议,开源的c/c++的http客户端框架,libcurl是首选,而且也 ...
- 基于libcurl的restfull接口 post posts get gets
头文件 #pragma once #ifndef __HTTP_CURL_H__ #define __HTTP_CURL_H__ #include <string> #include &q ...
- 基于libcurl的POST(http)
#include <stdio.h> #include <curl/curl.h> int main (void) { char *url="http://www.n ...
- C++ 用libcurl库进行http通讯网络编程
使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...
- C++ 用libcurl库进行http通讯网络编程(转)
转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...
- libcurl
一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.libcurl使用 ...
- C++ 用libcurl库进行http通讯网络编程[转]
http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...
- C/C++ 用libcurl库进行http通讯网络编程
C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...
- C++ 用libcurl库进行http 网络通讯编程
一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...
随机推荐
- R apply()函数
创建一个列表变量,它的第一个元素包含所有从0到9的平方数,第二个元素为10到19之内的所有平方数,依此类推,最后一个元素为90到99之内的平方数.没有平方数的元素也应该被包含在内! 学习网友的解题思路 ...
- 二十六、SAP中通过FORMAT COLOR来设置文字背景颜色
一.代码如下 二.效果如下
- 141-PHP类的抽象方法和继承实例(一)
<?php abstract class ren{ //定义人类 //定义成员属性 protected $name=''; protected $age=0; //定义成员方法 public f ...
- django ModelForm在模板中显示中文
情景再现 修改ModelForm 效果
- webpack散记--Typescript
Typescript 1.js的超集 官网:typescriptlang.org/tslang.cn 来自:微软 安装:官方的 npm i typescript ts-loader --save-d ...
- 编程入门-Eclipse的断点调试
编程入门-Eclipse的断点调试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.双击选中你要调试的代码行数 2>.允许方法透视图 3>.进行代码调试 4& ...
- VS2012中MFC 操作mshflexgrid插入图片
CPictureHolder pic,picSection; pic.CreateFromBitmap(IDB_BITMAP); LPDISPATCH pPic = pic.GetPictureDis ...
- 洛谷 P2370 yyy2015c01的U盘
题目传送门 解题思路: 先将每个文件按照占空间从小到大排序,然后跑背包,当到了某一个文件时,价值够了,那么当前文件的体积就是答案. 其实本题是可以二分答案的,但是写挂了... AC代码: #inclu ...
- 1 ~ express ~ 初始化。安装第三方模块express。中间件
一,初始化 二,安装第三方模块express 三,安装中间件 1,bodyParser : 解析 post 请求数据 2,cookies : 读写 cookie 3,swig :模板解析引擎 4,mo ...
- mybatis初步配置容易出现的问题
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...