android - 从 Webview 下载 .cer 文件

标签 android webview

我想知道 2 天了,但无法修复它。

我在 WebView 上打开了一个 URL。 网站上有一个 Button 可以下载 .cer 文件。

我的问题是当我点击那个按钮时它开始下载文件并在通知栏中通知我。

我正在尝试使用以下代码来执行此操作:

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);

设备的默认浏览器也发生了同样的情况,但我可以在以下时间下载文件 我在 firefox 浏览器中打开 url。

我也添加了权限

<!-- 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" />

在 list 文件中。

请帮助我。

最佳答案

您好,我终于成功下载了 .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());

我在 certResponseBody 中将我的 .cer 文件作为字符串保存在设备存储中。

干杯....

关于android - 从 Webview 下载 .cer 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836673/

相关文章:

android - 在 React Native 中调试 Android WebViews

android - 使用新的 Android API 跟踪 100 多个同步地理围栏

Android 返回栈和内存回收

android - 动画 ImageView 的位图位置

android - 使用 Google Play 应用签名 - 如何进行更新版本测试

Android Espresso 测试 : how test an activity's onNewIntent?

android - 如何在webview中获取缩放视口(viewport)的宽度和高度?

android - 嵌入在 Android Webview 中的 Youtube 视频不能以 SINGLE_COLUMN 布局播放

windows - 如何清除 UWP WebView 缓存?

webview - 是否可以在混合应用程序中使用 NativeScript 的 WebView 作为 WebMap?