通过C程序将文件复制到HTTP服务器
问题描述:
< HTTP/1.1 405 Method Not Allowed
即使我在curl API中使用POSt,它说PUT方法是不允许的。通过C程序将文件复制到HTTP服务器
< Date: Fri, 20 May 2016 11:03:06 GMT * Server Apache/2.2.15 (CentOS) is not blacklisted < Server: Apache/2.2.15 (CentOS) < Allow: GET,HEAD,POST,OPTIONS,TRACE < Content-Length: 318 < Connection: close < Content-Type: text/html; charset=iso-8859-1
得到上面的错误,当我尝试使用HTTP POST。以下是我正在尝试的代码
curl = curl_easy_init();
if(curl) {
/* upload to this place */
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
/* tell it to "upload" to the URL */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
/* set where to read from */
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* enable verbose for easier tracing */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
}
答
只留下CURLOPT_POST并移除CURLOPT_UPLOAD。见this question。
当您设置CURLOPT_UPLOAD将PUT方法“锁定”到句柄时,似乎libcURL会在内部执行某些操作,如this other question中所述,使得以下curl_easy_setopt与CURLOPT_POST无效。
我需要curl安装在运行http的服务器上吗? – r4nj4n
@Michael你有什么想法吗? – r4nj4n
您是否阅读过cURL文档?特别是https://curl.haxx.se/libcurl/c/libcurl-tutorial.html的'HTTP POSTing'部分似乎错过了代码中的一些东西 – GMichael