第一次curl用户,使用vc ++处理带有multipart/form-data
请求的curl文件上载。
经过研究,我找到了两种方法来提出multipart/form-data
请求。使用" CURLOPT_POSTFIELDS
"将值设置为数组或使用formadd并将指针传递给CURLOPT_POSTFIELDS
。我决定做第二个并使用" formdata"来配置请求的不同字段。 (两个字段,一个文件(application / sla)另一个origin(text / plain)
我使用wireshark来捕获请求,并看到CURL返回CURLE_READ_ERROR (26)
。然后我添加了CURLFORM_FILECONTENT," tmp_3DButton.stl"并得到了CURL_OK,但是我发送请求的服务器返回" Bad Media Type"我看到CURL没有把我的请求作为多部分发送..
(win8 64 bit,visual studio 2015,curl,c ++
CURL *curl;
CURLcode res;
char error[CURL_ERROR_SIZE];
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
curl_global_init(CURL_GLOBAL_ALL);
// Fill in the file upload field
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_CONTENTTYPE, "application/sla",
CURLFORM_FILECONTENT, "tmp.stl",
CURLFORM_FILE, filePathSTL,
CURLFORM_END);
// Fill in the filename field
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "origin",
CURLFORM_CONTENTTYPE, "text/plain",
CURLFORM_FILENAME, "se_1_0",
CURLFORM_END);
curl = curl_easy_init();
headerlist = curl_slist_append(headerlist, "Accept: text/plain");
if (curl) {
// URL that receives this POST
curl_easy_setopt(curl, CURLOPT_URL, "http://blabla.com/upload");
//multipart/form-data
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
2);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
error[0] = 0;
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
CString str;
// Check for errors
if (res != CURLE_OK)
{
str.Format("curl_easy_perform return %s [%d]", curl_easy_strerror(res), res);
size_t len = strlen(error);
if (len)
{
_bstr_t bstrt(error);
AfxMessageBox(error);
}
}
curl_easy_cleanup(curl);
filePathSTL是bstrt,例如C:\..\..\tmp.stl
(带有双倍斜杠..)
答案 0 :(得分:0)
找到答案," CURLFORM_FILE"显然想要一根绳子!