I am refering to the post "Can report data be accessed programatically?" and further conversation and had written below java class to export the csv to local drive.
Authentication to Salesforce was successful but the out csv is not correct. Please help me with the issue.
Java client class:
String url = https://cs24.salesforce.com/reportIdxxxxx?export=1&enc=UTF-8&xf=csv
URL obj = new URL(url);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
PROXY, PORT));
HttpURLConnection con = (HttpURLConnection) obj.openConnection(proxy);
con.setRequestMethod("GET");
con.setRequestProperty("Authorization","Bearer " + accessToken);
con.setRequestProperty("accept", "text/plain");
con.setRequestProperty("Content-Type", "text/csv; charset=utf-8");
con.setReadTimeout(15*1000);
con.connect();
int responseCode = con.getResponseCode();
System.out.println("responseCode: "+responseCode);
System.out.println("HttpURLConnection.HTTP_OK: "+HttpURLConnection.HTTP_OK);
if(HttpURLConnection.HTTP_OK == responseCode) {
String fileName = "";
String disposition = con.getHeaderField("Content-Disposition");
String contentType = con.getContentType();
int contentLenght = con.getContentLength();
System.out.println("disposition: "+disposition);
System.out.println("contentType: "+contentType);
System.out.println("contentLenght: "+contentLenght);
InputStream inputStream = con.getInputStream();
System.out.println("inputStream: "+inputStream);
String savefilePath = "D://Tickets"+File.separator+"test.csv";
FileOutputStream outputStream = new FileOutputStream(savefilePath);
int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0,bytesRead);
}
}
Output csv:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<html>
<head>
<head>
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<script>
<script>
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv'); } else
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv'); } else
if (window.location.replace){
if (window.location.replace){
window.location.replace('https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv');
window.location.replace('https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv');
} else {;
} else {;
window.location.href ='https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv';
window.location.href ='https://test.salesforce.com/?ec=302&startURL=%2F00OG0000007BCX6%3Fview%3Dd%26csv%3D1%26snip%3D%26enc%3DUTF-8%26export%3D1%26xf%3Dcsv';
}
}
</script>
</script>
</head>
</head>
</html>