我想以编程方式连接到具有用户名和密码的VPN Server。

请帮助我实现这一目标。

谢谢

最佳答案

试试这个可能对你有帮助

public String getUrlContent(String urlstring,final String user,final String password) throws IOException
    {
        byte[] rawbyte = null;
        URL url = new URL(urlstring);

        Authenticator.setDefault(new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password.toCharArray());
            }});
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setUseCaches(false);
        urlConnection.connect();
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            try
            {
                InputStream in = new BufferedInputStream(
                        urlConnection.getInputStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int c;
                while ((c = in.read()) != -1)
                {
                    out.write(c);
                }
                out.flush();

                rawbyte = out.toByteArray();

                urlConnection.disconnect();
                in.close();
                out.close();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return new String(rawbyte);
        }
        return null;
    }

10-08 06:44