I have a php URL, which when I run in a browser, it returns to me a PDF file. In my application I want to download this PDF file. I'm trying to do this:
HttpURLConnection urlConnection = urlConnection = (HttpURLConnection) url_download_relatorio_funcionario.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
int totalSizePDF = urlConnection.getContentLength();
But the integer value "totalSizePDF" only returns to me -1 value, instead of the real size of PDF file which is opened if I run this URL in a browser. How can I download this PDF file from the php URL, producing the same effect as a browser?
getContentLength()
uses the Content-Length
header which, according to Google Chrome's Dev Tools, your server isn't returning.
Try setting the length header from your PHP script first.
Downloading the file should be simply reading the returned bytes and saving them to file.