0

我需要为 Android 应用程序建立 SSL 连接。我已经尝试了我找到的每个示例,但没有一个可以在我的应用程序中使用。这是我第一次进行 SLL 连接,我不确定我做得好还是坏,所以有人可以告诉我一些手册、指南、书籍或类似的东西,其中 SSL 连接是从零开始解释的。谢谢你。

编辑 我需要一些指南来学习如何管理 SSL 连接,无论如何这是我正在使用的代码,我从 StackOverflow 获得它。

其中: url 是连接用户的 url,密码是字符串

 DefaultHttpClient http = new DefaultHttpClient();
    SSLSocketFactory ssl =  (SSLSocketFactory)http.getConnectionManager().getSchemeRegistry().getScheme( "https" ).getSocketFactory(); 
    ssl.setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );

    Credentials creds = new UsernamePasswordCredentials(username,password);
    http.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);


    HttpResponse res;

    try{
        HttpPost httpost = new HttpPost(url);
        httpost.setEntity(new StringEntity(request));
        res = http.execute(httpost);

        InputStream is = res.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while((current = bis.read()) != -1){
          baf.append((byte)current);
        }
        Log.d("resp","resp"+new String(baf.toByteArray()));
    }

    catch (ClientProtocolException e) {
       e.printStackTrace();
    } 
    catch (IOException e) {
       e.printStackTrace();
    }

事实上,我正在尝试做与 Android Ignoring HTTPS conexion相同的事情

4

0 回答 0