我想以编程方式连接到在 android 中具有用户名和密码的 VPN 服务器。
请帮助我实现这一目标。
谢谢
试试这个可能对你有帮助
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;
}
你可以试试xinkvpn,这里是github链接:https ://github.com/xinthink/xinkvpn