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