【发布时间】:2019-01-17 11:07:42
【问题描述】:
我有 cURL https 请求,我试图在 C++ 程序中发送到我的 Web 服务器。我收到了“错误请求”格式的响应。
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
int res = 0;
snprintf(curl_url, sizeof(curl_url), "https://%s:8080/hello", results);
snprintf(curl_fields, sizeof(curl_fields),"\"name\":\"%s\", \"key\":\"%s\"", name, data);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charset: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, curl_url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, curl_fields);
res = curl_easy_perform(curl);
if ( res )
{
---error---
}
curl_easy_cleanup(curl);
curl_global_cleanup();
}
我能得到一些帮助吗?
【问题讨论】:
-
curl_url是什么类型? -
在使用它们之前尝试打印出这些值(如您的 URL)以确保它们是正确的。
-
char[256] 用于 curl_url 和 curl_fields。
-
我注意到有时服务器需要
User-Agent标头。 -
charsets不是有效的 HTTP 请求标头。也许你的意思是Accept-Charset?