#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)的更多相关文章

  1. 基于libcurl实现REST风格http/https的get和post

    c/c++开发中经常要用到http/https协议,直接使用socket工作量很大,要是使用socket实现https,那更不可思议,开源的c/c++的http客户端框架,libcurl是首选,而且也 ...

  2. 基于libcurl的restfull接口 post posts get gets

    头文件 #pragma once #ifndef __HTTP_CURL_H__ #define __HTTP_CURL_H__ #include <string> #include &q ...

  3. 基于libcurl的POST(http)

    #include <stdio.h> #include <curl/curl.h> int main (void) { char *url="http://www.n ...

  4. C++ 用libcurl库进行http通讯网络编程

    使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...

  5. C++ 用libcurl库进行http通讯网络编程(转)

    转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...

  6. libcurl

    一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.libcurl使用 ...

  7. C++ 用libcurl库进行http通讯网络编程[转]

    http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...

  8. C/C++ 用libcurl库进行http通讯网络编程

    C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...

  9. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

随机推荐

  1. 官网英文版学习——RabbitMQ学习笔记(二)RabbitMQ安装

    一.安装RabbitMQ的依赖Erlang 要进行RabbitMQ学习,首先需要进行RabbitMQ服务的安装,安装我们可以根据官网指导进行http://www.rabbitmq.com/downlo ...

  2. 19 02 03 django 中cookies 和 session 和 cache

    Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cookies,再从服务器中检索是否有这个 session ...

  3. javascript中window.open()与window.location.href

    1.window.location是window对象的属性,而window.open是window对象的方法    window.location是你对当前浏览器窗口的URL地址对象的参考!      ...

  4. JAVA - SpringBoot项目跨域访问

    JAVA - SpringBoot添加支持CORS跨域访问 CORS(Cross-Origin Resource Sharing)“跨域资源共享”,是一个W3C标准,它允许浏览器向跨域服务器发送Aja ...

  5. Kylin笔记

    简介 Apache Kylin(Extreme OLAP Engine for Big Data)是一个开源的分布式 分析引擎,为Hadoop等大型分布式数据平台之上的超大规模数据集通过标准 SQL查 ...

  6. JavaWeb学习记录

    服务器端跳转(请求重定向): 1.jsp内跳转 : <jsp:forward page="page_scope_03.jsp"/> 客户端跳转(请求转发): 1.通过超 ...

  7. (java) webdriver 启动firefox driver时,加载firebug的扩展

    去网上下载一个firebug.xpi(对应版本, 我的ff是17,可以使用firebug-1.11.4.xpi,最好使用非firefox浏览器下载,不然提示你直接安装到firefox) @Before ...

  8. POJ 1426:Find The Multiple

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Su ...

  9. PHP实现简单的双色球机选号码

    <?php header('Content-Type: text/html; charset=utf-8'); //PHP实现双色球机选号码 $red = range(1, 33);//初次设定 ...

  10. Community Cloud零基础学习(三)Partner Account

    本篇参考:http://salesforce.vidyard.com/watch/bLE3QNRSej2iasw9vvc6Tk http://salesforce.vidyard.com/watch/ ...