0

我正在尝试将 Java 应用程序转换为 C++,我正在使用 cURL 来处理我的请求。下面是java代码;我想知道如何复制该connection.setRequestProperty()方法。

connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false); 
connection.setReadTimeout(10000);
String userId= =getUserId()
connection.setRequestProperty("UserID", userId);

以下是我当前不起作用的代码。

struct curl_slist *headers=NULL;
curl_slist_append(headers, "UserID="2"); 

curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers); 
curl_easy_setopt(curl, CURLOPT_URL,url.c_str());
curl_easy_setopt(curl,CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
curl_easy_setopt(curl, CURLOPT_CAINFO, certDataPath.c_str());
CURLcode c =curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, postRequestCallback); 

下面是一个失败的 java servlet 代码(id 为空或空)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
...
...
String ud = request.getHeader("UserID");
}

setRequestPropertycURL 中的等效命令是什么。

我确定我遗漏了一些明显的东西。

4

1 回答 1

2

可能是您的标头字符串格式已关闭,如何:

curl_slist_append(标题,“用户 ID:2”);

啊,你需要分配结果,所以

headers = curl_slist_append(headers, "UserID: 2");

于 2012-01-03T03:02:41.607 回答