小编abh*_*bhy的帖子

无法将PDF文件作为二进制数据获取

我正在尝试从以下位置获取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 …
Run Code Online (Sandbox Code Playgroud)

java android httprequest httpurlconnection http-headers

7
推荐指数
1
解决办法
3729
查看次数