0
我有以下捲曲,我要轉換爲Java代碼:轉換捲曲打電話到Java
curl -i -b 'JSESSIONID=ekl23l2-3321-1930-b889-kelwek23b8v9; Path=/; HttpOnly' http://127.0.0.1:8080/api/library
在上面的電話,JSESSIONID cookie是負責確保服務器驗證調用。我想出了以下Java代碼:
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/api/library");
httpGet.addHeader("Set-Cookie","JSESSIONID=ekl23l2-3321-1930-b889-kelwek23b8v9; Path=/; HttpOnly");
HttpResponse httpResponse = httpClient.execute(httpGet);
BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject o = new JSONObject(result.toString());
rd.close();
httpClient.close();
但是,這對我來說不起作用,因爲服務器抱怨未經授權的訪問。這個響應發生在用戶沒有被授權時,這意味着JSESSIONID沒有得到正確的設置。任何人都可以評論,如果這是將cURL翻譯成Java的正確方法嗎?
您是否試過尋找您的Web服務器日誌文件?通過接口窺探HTTP流量?有時候,當這樣的事情失敗時,它可能是一個非常細微的東西,它顯示在嗅探頭中,告訴你可能需要設置什麼標誌或功能。你可以用發送的curl HTTP數據包和發送的Java數據包更新你的問題嗎? – clearlight