abh*_*bhy 7 java android httprequest httpurlconnection http-headers
我正在尝试从以下位置获取PDF文件:
网址:https:// domain_name/xyz/_id/download /
其中它不指向直接pdf文件,并且下载每个唯一文件来解释特定的<_id>字段.
我把这个链接放在浏览器的地址栏中,然后立即下载Pdf文件,而当我尝试通过HTTPsURLConnection获取它时,它的Content-Type是'text/html'形式,而它应该是'application/pdf' .
在连接之前我还尝试'setRequestProperty'为'application/pdf',但文件总是以'text/html'形式下载.
我用它的方法是'GET'
1)我是否需要使用HttpClient而不是HttpsURLConnection?
2)这些类型的链接是否用于增加安全性?
3)请指出我的错误.
4)我怎么知道服务器上的文件名?
我粘贴在我实施的主要代码之下:
    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();
我搜索了很多,无法得到结果.如果可能的话,请尽量详细说明我的错误,因为我是第一次实施这件事.
**尝试获取直接的pdf链接,例如:http://labs.google.com/papers/bigtable-osdi06.pdf ,它们很容易下载,而且他们的'Content-Type'也是'application/pdf'**
谢谢.
该线程引导我找到了问题的解决方案!当您尝试从 WebView 下载流式 PDF 并使用 HttpURLConnection 时,您还需要从 Webview 中传递 cookie。
String cookie = CookieManager.getInstance().getCookie(url.toString());
if (cookie != null) connection.setRequestProperty("cookie", cookie);
| 归档时间: | 
 | 
| 查看次数: | 3729 次 | 
| 最近记录: |