我正在尝试将文件发布到API,然后检查响应中的各种情况。但是,每当我尝试发布大于0字节的文件时,都会出现错误:
First-chance exception at 0x77AADF63 (ntdll.dll) in Test.exe:
0xC0000005: Access violation writing location 0x00000014.
相关代码为:
std::string Network::Post(std::string url, std::map<std::string, std::string> requestBody, std::string file, int port) {
std::ostringstream response;
std::stringstream bodyStream;
struct stat file_info;
for (std::map<std::string, std::string>::iterator iterator = requestBody.begin(); iterator != requestBody.end(); iterator++) {
bodyStream << iterator->first << "=" << iterator->second << "&";
}
//Get file
FILE* file_handle = fopen(file.c_str(), "rb");
int t = fstat(_fileno(file_handle), &file_info);
CURL *ch;
curl_global_init(CURL_GLOBAL_ALL);
ch = curl_easy_init();
struct curl_slist *headers = NULL;
curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
std::string bodyStreamString = bodyStream.str();
const char* bodyStreamCharPtr = bodyStreamString.c_str();
curl_easy_setopt(ch, CURLOPT_URL, url.c_str());
curl_easy_setopt(ch, CURLOPT_PORT, port);
curl_easy_setopt(ch, CURLOPT_POSTFIELDS, bodyStreamCharPtr);
curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, bodyStream.str().length());
curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(ch, CURLOPT_COOKIEFILE, "cookies.dat");
curl_easy_setopt(ch, CURLOPT_COOKIEJAR, "cookies.dat");
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, &ResponseToString);
curl_easy_setopt(ch, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(ch, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(ch, CURLOPT_READDATA, file_handle);
curl_easy_setopt(ch, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(ch, CURLOPT_WRITEDATA, &response);
int res = curl_easy_perform(ch);
curl_easy_cleanup(ch);
return response.str();
}
size_t Network::ResponseToString(char *ptr, size_t size, size_t nmemb, void *stream) {
std::ostringstream *s = (std::ostringstream*)stream;
size_t count = size * nmemb;
s->write(ptr, count);
return count;
}
知道发生了什么吗?坚持了一天左右,叹了口气:(
更多细节:
它只是在“int res = curl_easy_perform(ch);”处中断。 ,无法介入。
如果我删除了WRITEFUNCTION和WRITEDATA选项,它可以工作,但无法获得响应。
这个问题似乎也不在ResponseToString方法中。
最佳答案
问题是您的WRITEFUNC。
我非常确定Network::ResponseToString不是静态的,这会引起问题,因为非静态函数会将“this”作为第一个参数传递,然后拧入函数参数,因此我建议完全避免使用类成员,并且使用它(与Network::Post放在同一文件中,并在Post方法定义之前):
static size_t ResponseToString(char *ptr, size_t size, size_t nmemb, std::ostringstream *stream) {
size_t count = size * nmemb;
stream->write(ptr, count);
return count;
}
记住也要更改:
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, &ResponseToString);
在:
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ResponseToString);
另外,如果您的作用域是一个简单的“post”,则不需要在curl opt声明中指定的许多 header :
curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, bodyStream.str().length());
curl_easy_setopt(ch, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(ch, CURLOPT_READDATA, file_handle);
curl_easy_setopt(ch, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
关于c++ - C++ CURL发布文件,然后得到响应错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32244887/