首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Webview下载.cer文件

从Webview下载.cer文件
EN

Stack Overflow用户
提问于 2014-04-03 11:45:04
回答 1查看 145关注 0票数 1

我想知道已经有两天了,但没能修好它。

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

我的问题是,当我点击那个Button时,它开始下载文件并在Notification Bar中通知我。

我在下面的代码中尝试这样做:

代码语言:javascript
复制
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时,我可以下载文件。

我还增加了权限

代码语言:javascript
复制
<!-- 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" />

在清单文件中。

请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 05:00:30

嗨,我终于成功下载了.cer文件。

对我起作用的代码是:

代码语言:javascript
复制
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文件。

干杯..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22836673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档