如何使用来自Android应用程序HTTPS休息服务 [英] How to consume HTTPS rest service from android application

查看:179
本文介绍了如何使用来自Android应用程序HTTPS休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常新的Andr​​oid应用程序的开发。我工作的一个示范项目,在这里我想用我的Andr​​oid应用程序作为C#.NET创建一个WCF REST服务的客户端。该服务已托管在一个互联网服务器和工作正常,因为我现在用的是相同的服务在我的其他.NET Web应用程序(如客户端)。 但是,当我试图从我的Andr​​oid应用程序访问相同的REST服务(返回一个JSON对象),它抛出以下异常。

I am very much new to android application development. I am working on a demo project, where I am trying to use my android application as a client of a WCF REST service created on C#.net. The service is already hosted in a internet server and is working fine as I am using the same service in my other .Net web applications (as client). But when I am trying to access the same REST service (returning a JSON object) from my android application it’s throwing following exception.

产生java.io.IOException:SSL握手失败:失败的SSL库,通常一个协议错误 错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议(外部/ OpenSSL的/ SSL / s23_clnt.c:585 0xaf586674:00000000)

"java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:585 0xaf586674:0x00000000)"

以下是code口用于连接服务。

Following is the code I used to connect the service.

final String url = "https://mywebsite.com/service/myservice.svc/userid/" + usrid + "/" + password + "/authenticate";

        Uri uri = Uri.parse(url);
        HttpClient httpclient = new DefaultHttpClient();
        HttpHost host = new HttpHost(uri.getHost(), 443, uri.getScheme());
        HttpPost httppost = new HttpPost(uri.getPath());
        try {
            HttpResponse response = httpclient.execute(host, httppost);  // Throwing exception on this line
               HttpEntity entity = response.getEntity();
            if (entity != null) { 
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                JSONArray nameArray=json.names();
                JSONArray valArray=json.toJSONArray(nameArray);
                for(int i=0;i<valArray.length();i++)
                {
                    nameArray.getString(i);
                } 
                instream.close();
            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我应该要以忽略SSL证书错误,请确认创建我自己的TrustManager的SSLContext。如果是的话,请你提供code的例子也是如此。

Should I need to create a SSLContext with my own TrustManager in order to ignore the SSL certificate error please confirm. If yes, could you please provide the code example as well.

推荐答案

这是你所提到的将工作在真实设备的code。你presented的问题是由防火墙在你的电脑引起的阻塞443端口。

The code that you have mentioned will work in a real device. The problem that you presented is caused by the firewall in your PC that blocks the 443 port.

尝试禁用防火墙,并尝试在仿真器中的应用程序。我相信它会工作。

Try to disable your firewall and try your application in the emulator. I believe it will work.

这篇关于如何使用来自Android应用程序HTTPS休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆