簡體   English   中英

從java轉換為c ++,setRequestProperty的等效curl命令是什么

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

我正在嘗試將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的equiv命令是什么。

我確定我錯過了一些明顯的東西。

你的標題字符串格式可能會關閉,怎么樣:

curl_slist_append(headers,“UserID:2”);

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

headers = curl_slist_append(headers,“UserID:2”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM