Android开发:如何通过https创建网络登录

时间:2012-11-28 11:35:24

标签: java android https port

我在网上搜索过但无法找到问题的答案。

我要求我的Android应用程序连接到我的网络服务器。

示例https://SERVER-ADDRESS:PORT/

我该怎么做?

1 个答案:

答案 0 :(得分:0)

try{
String url = "https://SERVER-ADDRESS:PORT/site/login?username=" + user + "&password=" + password;
HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 1000 * 60 * 2);
        HttpConnectionParams.setSoTimeout(params, 0);
        HttpClient httpClient = new DefaultHttpClient(params);

        //prepare the HTTP GET call 
        HttpGet httpget = new HttpGet(url);
        //get the response entity
        HttpEntity entity = httpClient.execute(httpget).getEntity();

        if (entity != null) {
            //get the response content as a string
            String response = EntityUtils.toString(entity);
            //consume the entity
            entity.consumeContent();

            // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
            httpClient.getConnectionManager().shutdown();
           }
    }catch (Exception e) {
        e.printStackTrace();
    }