我正在嘗試使用libcurl和C 通過POST請求向一個遠程位置上傳一個檔案。然而,我認為我做錯了什么,因為我被告知檔案沒有到達另一邊。
我正在使用下面的代碼:
使用 命名空間 std;
size_t WriteCallback(void * buffer。size_t size, size_t count, void * user)
{
((string *) user)->append((char *) buffer, 0, size * count) 。
return size * count;
}
int main()
{
curl_global_init(CURL_GLOBAL_ALL)。
CURL * Curl;
CURLCode res;
Curl = curl_easy_init()。
curl_easy_setopt(Curl, CURLOPT_FAILONERROR, 0)。
curl_easy_setopt(Curl, CURLOPT_VERBOSE, 1L) 。
struct curl_httppost * formpost = NULL。
struct curl_httppost * lastptr = NULL。
struct curl_slist * headerlist = NULL。
static const char buf[] = "Expect:"/span>;
ImageName = "myimage.jpg"/span>
ImageNameWithPath = "/this/location/right/here/" ImageName;
curl_formadd(& formpost,
& lastptr,
CURLFORM_COPYNAME, ImageName.c_str();
CURLFORM_FILE, ImageNameWithPath.c_str()。
CURLFORM_CONTENTTYPE, "image/jpeg (binary)";
CURLFORM_END)。)
curl_formadd(& formpost,
& lastptr,
CURLFORM_COPYNAME, ImageName.c_str()。
CURLFORM_COPYCONTENTS, ImageNameWithPath.c_str();
CURLFORM_CONTENTTYPE, "image/jpeg (binary)";
CURLFORM_END)。)
headerlist = curl_slist_append(headerlist, buf)。
string MessageBodyLine1 = "Content-Disposition: form-data; name="file"; filename""/span> ImageName ""/span>。
headerlist = curl_slist_append(headerlist, MessageBodyLine1.c_str()。
string Url = https://www.example.com/ // There is a real URL here.
curl_easy_setopt(Curl, CURLOPT_URL, Ulr.c_str() )。
curl_easy_setopt(Curl, CURLOPT_HTTPHEADER, headerlist)。
curl_easy_setopt(Curl, CURLOPT_HTTPPOST, formpost)。
字串 Reponse;
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, WriteCallback)。
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, & Response)。
res = curl_easy_perform(Curl)。
curl_easy_cleanup(Curl)。
curl_formfree(formpost)。
curl_slist_free_all(headerlist)。
curl_global_cleanup()。
return 0;
實際上,我期望輸出的東西看起來像這樣的地方
POST URL HTTP/1.1
Content-Type: multipart/form-data; boundary=----BoundaryString
----BoundaryString
Content-Disposition: form-data; name="file"; filename="myfile.jpg"。
Content-Type: image/jpeg (binary)
而我得到的是:
* Trying IP
*連接到IP埠 PORT (#0)
發布網址 http/1.1
主機。IP
接受。IP:埠
Content-Disposition: form-data; name="file"; filename="myfile.jpg"。
內容-長度: totalsize
Content-Type: multipart/form-data; boundary=----BoundaryString
< HTTP/1.1 200 OK
[...]
現在它說OK,但我知道事實是,影像沒有到達另一邊。此外,我的Response字串也是空的,而它應該包含一個JSON字串。
我到底做錯了什么?不幸的是,我被困在一個舊的libcurl版本中,因此不能使用curl 7.55中的mime格式。
uj5u.com熱心網友回復:
不要將Content-Disposition請求頭添加到HTTP請求的headerlist中,它不屬于那里。它屬于每個MIME部分(即,你應該使用CURLFORM_COPYNAME, "file"和CURLFORM_FILE, ImageName.c_str())。我希望curl_formadd()能夠為你處理Content-Disposition,你應該不需要手動創建它。
另外,image/jpeg (binary)不是一個有效的Content-Type值,它需要的只是image/jpeg。
還有,為什么你為同一個檔案呼叫curl_formadd()兩次,一次是CURLFORM_FILE,另一次是CURLFORM_COPYTENTS?特別是你對CURLFORM_COPYTENTS的輸入是錯誤的(它期望一個指向實際資料的指標,而不是一個指向檔案名字串的指標)。在這種情況下,你應該只使用CURLFORM_FILE。
試一下:
#include "curl/curl.h"
#include <string>
using namespace std;
size_t WriteCallback(void * buffer。size_t size, size_t count, void * user)
{
size_t numBytes = size * count;
static_cast<string*>(user)->append(static_cast< char*>(buffer), 0, numBytes) 。
return numBytes。
}
int main()
{
curl_global_init(CURL_GLOBAL_ALL)。
CURL *Curl = curl_easy_init()。
if (! Curl)
return 1;
curl_easy_setopt(Curl, CURLOPT_FAILONERROR, 0)。
curl_easy_setopt(Curl, CURLOPT_VERBOSE, 1L) 。
curl_httppost *formpost = NULL;
curl_httppost *lastptr = NULL;
string ImageName = "myimage.jpg";
string ImageNameWithPath = "/this/location/right/here/" ImageName;
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_FILE, ImageNameWithPath.c_str()。
CURLFORM_CONTENTTYPE, "image/jpeg"/span>,
CURLFORM_END)。)
curl_slist *headerlist = curl_slist_append(NULL, "Expect:") 。
string Url = https://www.example.com/ //這里有一個真實的URL。
curl_easy_setopt(Curl, CURLOPT_URL, Url.c_str() )。
curl_easy_setopt(Curl, CURLOPT_HTTPHEADER, headerlist)。
curl_easy_setopt(Curl, CURLOPT_HTTPPOST, formpost)。
字串 Reponse;
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, WriteCallback)。
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, & Response)。
CURLCode res = curl_easy_perform(Curl)。
curl_easy_cleanup(Curl)。
curl_formfree(formpost)。
curl_slist_free_all(headerlist)。
curl_global_cleanup()。
return 0;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/331035.html
標籤:
