gpt4 book ai didi

java - 从 java 转换为 c++,setRequestProperty 的等效 curl 命令是什么

转载 作者:行者123 更新时间:2023-11-28 03:42:26 25 4
gpt4 key购买 nike

我正在尝试将 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 为 null 或空)

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

cURL 中 setRequestProperty 的等效命令是什么。

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

最佳答案

可能是你的标题字符串格式不对,怎么样:

curl_slist_append(headers, "UserID: 2");

啊,你需要给结果赋值,所以

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

关于java - 从 java 转换为 c++,setRequestProperty 的等效 curl 命令是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com