struct curl_httppost* formpost = NULL;
struct curl_httppost* lastptr = NULL;
//传递背景图
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "uploaded_image",
CURLFORM_FILE, par._uploaded_image.c_str(),
CURLFORM_END);
if (!par._style_image.empty()) {
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "style_image",
CURLFORM_FILE, par._style_image.c_str(),
CURLFORM_END);
}
std::string strStyle = std::to_string((_Longlong)par._style_enum);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "style_enum",
CURLFORM_COPYCONTENTS, strStyle.c_str(),
CURLFORM_END);
std::string strStyleWeight = std::to_string((long double)par._style_weight);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "style_weight",
CURLFORM_COPYCONTENTS, strStyleWeight.c_str(),
CURLFORM_END);
struct curl_slist* headerlist = NULL;
static const char buf[] = "Expect:";
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
CURL* curl = curl_easy_init();
if (curl) {
/* what URL that receives this POST */
std::string strUrl;
if (par._request_type != "images_to_3d" && par._request_type != "text_to_3d")
strUrl = m_strUrl + "/upload-image";
else
strUrl = "http://192.168.0.64:10080/3d";
DPrintf("PostRequest strUrl=%s request_type=%s request_p_prompt=%s\n", strUrl.c_str(), par._request_type.c_str(), par._p_prompt.c_str());
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
//if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
/* only disable 100-continue header if explicitly requested */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteDataCallback);
int timeout = 600;
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
std::string strResponse = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
//struct curl_slist* headers = NULL;
//headers = curl_slist_append(headers, "User-Agent: Apipost/8 (https://www.apipost.cn)");
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
DPrintf("错误状态 res =%d", res);
if (res != CURLE_OK) {
//strError = curl_easy_strerror(res);
if (CURLE_HTTP2 > res) {
strError = _T("无法连接到主机或代理!");
}
else {
strError = _T("读取数据发生未知错误!");
}
}
这段代码中是一个请求传递,传递的表单第一个数据uploaded_image是一个文件地址,但是这个文件地址有时候 对传入的中午路径,请求返回26的错误;但是对个别中午路径却可以正确传递过去,现在可以保证的是文件是存在的并且文件路径没有错误,该如何解决这个问题