2016-05-12 117 views
3
< 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)); 

    } 
} 
+0

我需要curl安裝在運行http的服務器上嗎? – r4nj4n

+0

@Michael你有什麼想法嗎? – r4nj4n

+0

您是否閱讀過cURL文檔?特別是https://curl.haxx.se/libcurl/c/libcurl-tutorial.html的'HTTP POSTing'部分似乎錯過了代碼中的一些東西 – GMichael

回答

0

只留下CURLOPT_POST並移除CURLOPT_UPLOAD。見this question

當您設置CURLOPT_UPLOAD將PUT方法「鎖定」到句柄時,似乎libcURL會在內部執行某些操作,如this other question中所述,使得以下curl_easy_setopt與CURLOPT_POST無效。

相關問題