简体   繁体   中英

Converting from java to c++, what is the equivalent curl command for setRequestProperty

I'm trying to convert a Java application to C++, I am using cURL for my requests. Below is the java code; I'm wondering how to replicate the connection.setRequestProperty() method.

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

Below is my current code that does not work.

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); 

Below is a the java servlet code that is failing (id is null or empty)

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

What is the equiv command to setRequestProperty in cURL.

I'm sure I am missing something obvious.

Might be your header string format is off, how about:

curl_slist_append(headers, "UserID: 2");

ah, you need to assign the result, so

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM