2012-01-03 149 views
0

我試圖將Java應用程序轉換爲C++,我使用cURL來處理我的請求。 下面是java代碼;我想知道如何複製connection.setRequestProperty()方法。從java轉換爲C++,setRequestProperty的等效curl命令是什麼

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

下面是失敗(ID爲空或空)

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

什麼是當量命令在捲曲setRequestProperty一個在Java servlet代碼。

我確定我缺少一些明顯的東西。

回答

2

可能是您的標題字符串格式是關閉的,怎麼樣:

curl_slist_append(標題, 「用戶ID:2」);

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

標題= curl_slist_append(頭, 「用戶名:2」);

+0

好的,我試過curl_slist_append(headers,「UserID:2」);不幸的是結果相同 – user1127081 2012-01-03 03:16:49

+0

headers = curl_slist_append(headers,「UserID:2」); - 解決了我的問題,謝謝 – user1127081 2012-01-03 18:10:56