#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

最新文章

  1. U3D 打包时找不到tag的问题
  2. 如何基于纯GDI实现alpha通道的矢量和文字绘制
  3. hdu1018
  4. Filter的用法之注解
  5. 【转】SAPI中的IspeechRecoContext(接口)
  6. BZOJ 2561 最小生成树(最大流)
  7. 新手留言薄asp.net MVC 学习(适合新手学习)
  8. 从 setNeedsLayout 说起
  9. 正则表达式(.NET)
  10. NodeJS with Express 4.x
  11. 程序启动读取和关闭时保存应用程序设置(QSettings)
  12. 怎样取json对应的值
  13. linux开发常用命令
  14. Vcenter 账户密码过期设置修改
  15. 【转】python模块导入细节
  16. springcloud-3:required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
  17. Bootstrap-按钮相关的class
  18. oracle 排序 row_number() over(partition by 排序字段)
  19. Introduction to dnorm, pnorm, qnorm, and rnorm for new biostatisticians
  20. centos6上安装jenkins

热门文章

  1. JavaWeb高级编程(下篇)
  2. centos7下使用yum安装ifconfig工具
  3. Z-function
  4. Java compare方法和compareTo方法
  5. MFC 选择文件夹
  6. ntp时间同步服务器
  7. rdlc报表带参数打印
  8. 全面掌握Nginx配置+快速搭建高可用架构 一 开启status页面检测服务状态
  9. UVA 11997 The K smallest Sums
  10. 海外Essay写作如何减少重复用词