通过C程序将文件复制到HTTP Server

时间:2016-05-12 07:36:54

标签: c curl

< HTTP/1.1 405 Method Not Allowed

即使我在curl API中使用POSt,也就是说不允许使用PUT方法。

< 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));

    }
}

1 个答案:

答案 0 :(得分:0)

只保留CURLOPT_POST并删除CURLOPT_UPLOAD。请参阅this question

当你设置CURLOPT_UPLOAD将PUT方法“锁定”到句柄时,libcURL似乎在内部执行某些操作,如this other question中所述,使得以下curl_easy_setopt与CURLOPT_POST无效。