小编Ahm*_*mad的帖子

在android 4.0上以编程方式设置VPN连接

我正在以编程方式在Android设备上设置VPN连接.我成功地使用OS 2.3.5和之前的设备(我使用反射来访问隐藏的类).但是在android 4.0中,他们摆脱了旧类并使用了VPNService类.

我认为最好的起点是使用Android提供的ToyVPN示例,但我面临很多挑战.在示例代码中,他们只需要发送服务器地址:

InetSocketAddress server = new InetSocketAddress(mServerAddress, Integer.parseInt(mServerPort));
Run Code Online (Sandbox Code Playgroud)

然后通过打开频道创建VPN隧道:

tunnel = DatagramChannel.open();
Run Code Online (Sandbox Code Playgroud)

但在我的情况下,我需要发送服务器地址,用户名和密码.到目前为止,我还没有想出如何这样做.我最好的猜测是做这样的事情:

Authenticator.setDefault(new Authenticator(){
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user","pass".toCharArray());
        }});

    try {
        // Create a DatagramChannel as the VPN tunnel.

        tunnel = DatagramChannel.open();
Run Code Online (Sandbox Code Playgroud)

但这没效果.所以我要问的是:

  1. 除了ToyVpn中使用的方法之外,还有一种方法可以通过编程方式创建VPN连接吗?
  2. 如果没有,当我想建立与服务器的连接时,如何发送凭据?

编辑

我忘了提到我需要指定VPN类型(PPTP,L2TP,L2TP/IPSec PSK或L2TP/IPSec CRT).

vpn android android-4.0-ice-cream-sandwich

23
推荐指数
1
解决办法
9287
查看次数