简体   繁体   English

使用Android通过HTTP进行POST请求

[英]POST request over HTTPs with Android

I've got a big problem. 我有一个大问题。 I've to POST to an URL some parameters from my application. 我必须将应用程序中的一些参数发布到URL。 But when I try to do it within a WebView object it trows me an exception that says "Untrusted Certicate" (the exception is trown by the method in override of the WebViewClient onReceivedSslError()). 但是,当我尝试在WebView对象中执行此操作时,它抛出了一个异常,提示“不受信任的证书”(该异常被覆盖WebViewClient onReceivedSslError()的方法所抛出)。 How can I be able to handshake correctly with the server? 如何才能与服务器正确握手? Could you please give me some tips? 你能给我一些提示吗? I'm getting crazy... 我快疯了...

Really, really thanks... 真的,真的,谢谢...

EDIT: this is how I've defined my webview 编辑:这就是我定义我的webview的方式

webView = (WebView) myFragmentView.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String string){
            Log.debug(tag, "URL pagina terminata :"+webView.getUrl() );
            if(progress!=null) 
                if(progress.isShowing()){progress.dismiss();}
        }

        @Override
        public void onReceivedError(WebView view, 
                int errorCode,
                String description, 
                String failingUrl) {
            Log.error(tag, "ERROR:" + description );
           }

        @Override
        public void onReceivedSslError(WebView view,
                SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            Log.error(tag, "SSL Error received: "+ error.getPrimaryError());

            handler.proceed();
        }


    });

This is how I've created a postRequest within a WebView 这就是我在WebView中创建postRequest的方式

webView.postUrl(url, EncodingUtils.getBytes(postParameters, "base64"));

This is the LogCat output: 这是LogCat的输出:

SSL Error received: 3 - Untrusted Certificate

Now, googling a little bit I've seen that there's the need to check the validity of the certificate and to import the keychain into a local keystore. 现在,通过谷歌搜索,我发现有必要检查证书的有效性并将密钥链导入本地密钥库中。 But the problem is that I'0 don't know where to start... :) 但是问题是我不知道从哪里开始... :)

If security is not a priority, you can try to call handler.proceed() in your onReceivedSslError() , Don't call the super method. 如果安全不是一个优先级,你可以尝试调用handler.proceed()在你的onReceivedSslError()不要调用super方法。

If security is a priority (and it should be): 如果安全是当务之急 (应该如此):

I haven't tried this myself for webView, but try setting your certificate like this: Android WebView setCertificate issues SSL problems 我尚未尝试将其用于webView,但尝试按以下方式设置您的证书: Android WebView setCertificate问题SSL问题

To get the actual certificates you can load them into your browser (by accepting the warning when you open the url in the browser) and the export them from there, or you can use the openssl tool like described here: http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html 要获取实际的证书,您可以将它们加载到浏览器中(通过在浏览器中打开URL时接受警告)并从那里导出它们,或者可以使用如下所述的openssl工具: http:// blog。 crazybob.org/2010/02/android-trusting-ssl-certificates.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM