我有一些代码,以存储/读取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只是为客户端实现东西,而不是服务器。 –