http c语言使用方法,如何设置HTTP使用C语言编程

我有一些代码,以存储/读取HTTP头,但在我的应用我想设置并发送一个HTTP响应头为我的客户端头字段。那么如何为客户端发送的任何请求设置一个http头响应呢?如何设置HTTP使用C语言编程

这里是我的代码:

#include

#include

#include

#include

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)

{

int written = fwrite(ptr, size, nmemb, (FILE *)stream);

return written;

}

int main(void)

{

CURL *curl_handle;

static const char *headerfilename = "head.txt";

FILE *headerfile;

static const char *bodyfilename = "body.txt";

FILE *bodyfile;

curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */

curl_handle = curl_easy_init();

/* set URL to get */

curl_easy_setopt(curl_handle, CURLOPT_URL, "http://google.co.in");

/* no progress meter please */

curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

/* send all data to this function */

curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

/* open the files */

headerfile = fopen(headerfilename,"wb");

if (headerfile == NULL) {

curl_easy_cleanup(curl_handle);

return -1;

}

bodyfile = fopen(bodyfilename,"wb");

if (bodyfile == NULL) {

curl_easy_cleanup(curl_handle);

return -1;

}

/* we want the headers be written to this file handle */

curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);

/* we want the body be written to this file handle instead of stdout */

curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);

/* get it! */

curl_easy_perform(curl_handle);

/* close the header file */

fclose(headerfile);

/* close the body file */

fclose(bodyfile);

/* cleanup curl stuff */

curl_easy_cleanup(curl_handle);

return 0;

}

2013-09-24

Saagar

+0

* “为任何请求” * - > http://www.w3.org/Protocols/rfc2616/rfc2616.html –

+1

卷曲带有一些实例中,例如http://curl.haxx.se/libcurl/c/httpcustomheader.html,你需要什么? –

+0

@nos纠正我,如果我错了,但这只是为了修改客户端请求,而不是为了编写服务器响应。 AFAIK,curl只是为客户端实现东西,而不是服务器。 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值