Java implementation of random switch VPN change the Internet area _java

Source: Internet
Author: User

In many cases, the need for some network applications will require the simulator to access sites and applications in different regions. So switching IP also came into being, however, IP as a scarce resource is not freely available. It would be thought of the application switching VPN to reach different regions of the country to access the network. As a result, the following programmes are available.

Premise:

1. Windows operating system
2. Create a new network connection by hand

Package com.selenium.test;
Import Java.io.BufferedReader;
Import Java.io.InputStream;

Import Java.io.InputStreamReader; public class Connectnetwork {/** * @param args * @throws Exception/public static void Main (string[) arg 
    s) throws Exception {//TODO auto-generated Method Stub connadsl ("Vpn_test", "Test", "test"); 
    Thread.Sleep (1000); 
    CUTADSL ("Vpn_test"); 
  Thread.Sleep (1000); /** * Executes the cmd command and returns a string string */public static string Executecmd (String strcmd) throws Exception {System
    . OUT.PRINTLN ("cmd/c" + strcmd); 
    Process p = runtime.getruntime (). EXEC ("cmd/c" + strcmd);
    StringBuilder sbcmd = new StringBuilder ();
    Here is important, set GB2312 to solve garbled!!! 
        If the program default encoding is GB2312, you can not write//I netbeans default with UTF8 bufferedreader br = new BufferedReader (new InputStreamReader p 
    . getInputStream (), "GB2312")); 
    String Line; 
    while (line = Br.readline ())!= null) {sbcmd.append (line + \ n)); Return Sbcmd. toString (); ////If the whole process is replaced by this, it will be clearer.
    getInputStream is to obtain the most original byte stream,//cmd returns a byte stream encoded by GB2312 inputstream in = P.getinputstream ();
    Byte[] B = new byte[2000];
    In.read (b);
    String msg = new String (b, "GB2312");
    With GB2312 interpretation of this heap of bytes, you can assemble into a normal string//If the top does not write GB2312, equal to the UTF8 assembled here, the result is the same as return msg; 
   /}/** * Connect ADSL * Syntax: rasdial connection name Username password * instance: rasdial my broadband hzhz1234567890 dfdfdfdfdf */public static Boolean CONNADSL (String adsltitle, String adslname, String adslpass) throws Exception {SYSTEM.O 
    UT.PRINTLN ("The connection is being established."); 
    String adslcmd = "rasdial" + Adsltitle + "" + Adslname + "" + adslpass; 
    String tempcmd = Executecmd (adslcmd); 
    
    String tempcmd = Executecmd ("ping www.youku.com"); 
      Determines whether the connection succeeds if (Tempcmd.indexof (connected) > 0) {System.out.println ("The connection was successfully established."); 
    return true; 
      else {System.err.println (tempcmd); 
      SYSTEM.ERR.PRINTLN ("Establish connection failed"); Return false; }/** * Disconnect ADSL/public static Boolean CUTADSL (String adsltitle) throws Exception {string Cuta 
    DSL = "Rasdial" + adsltitle + "/disconnect"; 
     
    String result = Executecmd (CUTADSL); 
      if (Result.indexof ("no Connection")!=-1) {System.err.println (adsltitle + "Connection does not exist!"); 
    return false; 
      else {System.out.println ("disconnected"); 
    return true;

 } 
  } 
}

Description: The above code fragment is actually called the Windows rasdial command. So the main credit is that Rasdial,java is just a shell. Another curve to save the nation. Do we have a better way? If there's a better way, please tell me, ^_^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.