I am trying to extract a pdf file from:
URL: https: // domain_name / xyz / _id / download /
where it does not point to a direct PDF file, and each unique file is loaded with an interpretation of a specific field <_id.
I put this link in the address bar of the browser, and the Pdf file is downloaded instantly, while when I try to get it through HTTPsURLConnection, its Content-Type is in the form "text / html", while it should be in the / application pdf.
I also tried "setRequestProperty" to "application / pdf" before connecting, but the file was always uploaded to the "text / html" form.
The method I use is "GET"
1) Do I need to use HttpClient instead of HttpsURLConnection?
2) Are these types of links used to enhance security?
3) Please indicate my mistakes.
4) How to find out the file name on the server?
I insert below the main codes that I implemented:
URL url = new URL(sb.toString()); //created new connection HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); //have set the request method and property urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Content-Type", "application/pdf"); Log.e("Content Type--->", urlConnection.getContentType()+" "+ urlConnection.getResponseCode()+" "+ urlConnection.getResponseMessage()+" "+urlConnection.getHeaderField("Content-Type")); //and connecting! urlConnection.connect(); //setting the path where we want to save the file //in this case, going to save it on the root directory of the //sd card. File SDCardRoot = Environment.getExternalStorageDirectory(); //created a new file, specifying the path, and the filename File file = new File(SDCardRoot,"example.pdf"); if((Environment.getExternalStorageState()).equals(Environment.MEDIA_MOUNTED_READ_ONLY)) //writing the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); //variable to store total downloaded bytes Log.e("Total File Size ---->", ""+totalSize); int downloadedSize = 0; //create a buffer... byte[] buffer = new byte[1024]; int bufferLength = 0; //used to store a temporary size of the buffer //Reading through the input buffer and write the contents to the file while ( (bufferLength = inputStream.read(buffer)) > 0 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //adding up the size downloadedSize += bufferLength; //reporting the progress: Log.e("This much downloaded---->",""+ downloadedSize); } //closed the output stream fileOutput.close();
I searched a lot and could not get the result. If possible, try clarifying my mistake, as I am implementing this thing for the first time.
** I tried direct direct links: http://labs.google.com/papers/bigtable-osdi06.pdf and they load easily, and their "Content-Type" was also "application / pdf" **
Thanks.