【问题标题】:Converting from java to c++, what is the equivalent curl command for setRequestProperty从java转换为c++,setRequestProperty的等效curl命令是什么
【发布时间】:2012-02-01 04:59:12
【问题描述】:

我正在尝试将 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 的等效命令是什么。

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

【问题讨论】:

    标签: java c++ curl curlpp


    【解决方案1】:

    可能是你的标题字符串格式关闭了,怎么样:

    curl_slist_append(headers, "UserID: 2");

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

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

    【讨论】:

    • 好的,我试过 curl_slist_append(headers, "UserID: 2");不幸的是,结果相同。
    • headers = curl_slist_append(headers, "UserID: 2"); - 解决了我的问题,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多