我想知道自2天以来但无法修复它。
我在WebView
内打开了一个网址。
网站中有一个Button
可下载 .cer 文件。
我的问题是当我点击Button
它开始下载文件并通过Notification Bar
通知我。
我正在尝试下面的代码来执行此操作:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(sourceURL));
// appears the same in Notification bar while downloading
request.setTitle("Downloading");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
//DownloadManager.Request request = new DownloadManager.Request(source);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// Use the same file name for the destination
File destinationFile = new File(directory, "/Download/test.cer");
if (destinationFile.exists()) {
destinationFile.delete();
}
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
referencepath = manager.enqueue(request);
与Device的默认浏览器相同但我可以在下载文件时 我在firefox浏览器中打开网址。
我还添加了权限
<!-- Permission so that application can access Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission so that application can write on device storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单文件中。
请帮帮我。
答案 0 :(得分:1)
您好,我最终成功下载了.cer文件。
对我有用的代码是:
String sourceURL = url;
String directoryPath = directoryPath;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(sourceURL);
try {
/**
* getting Session Id from URL which is in Webview
*/
String sessionId = CookieManager.getInstance().getCookie(
sourceURL.toString());
if (sessionId != null)
httpPost.addHeader("Cookie", sessionId);
HttpResponse execute = client.execute(httpPost);
String certResponseBody = EntityUtils.toString(execute
.getEntity());
我将.cer文件作为certResponseBody中的字符串获取,我将其保存为设备存储中的.cer文件。
...干杯