In many cases, the needs of some network applications will require simulated people to visit websites and applications in different regions. Therefore, switching IP was born, but IP, as a scarce resource, is not obtained casually. Therefore, it is thought that the application switches VPN to achieve access to the network in different regions of the country. Therefore, there is the following plan.
premise:
1. Windows operating system
2. Create a new network connection manually
package com.selenium.test; Import Java.io.BufferedReader; Import Java.Io.inputStream; Import Java.Io.inputStreamReamReader; Public Class ConnectNetwork { / / / ** * @param ARGS * @throws Exception */ Public Static Void Main ( String [] ARGS) Throws Exception {// Todo Auto-Genious Method Stub Connadsl ("vpn_test", "test", "test"); thread.sleep (1000); "vpn_teest"); Thread.sleep (1000 );} /*** execute the CMD command, and return the String string* /Public Static String Executecmd (String Strcmd) Throws Exception {System.Println ("CMD /C" + Strcmd). ; Process P = Runtime.getruntime () .exec ("cmd /c" + strcmd); stringbuilder sbcmd = new stringbuilder (); // Here is very important, set gb2312 to solve garbled code! Intersection Intersection // If the program is CB2312 by default, you can write without writing. ; WHILE ((line = br. Readline ())! = Null) {sbcmd.append (line + " /n");} Return sbcmd.tostring (); /* // If the whole process is replaced, it will be clearer. GetInputStream is the most primitive byte stream, // CMD returns the byte stream intestream in the GB2312 dual byte encoded inputStream in = p.GetinputStream (); byte [] b = new byte [2000]; b); string msg = new string (b, "gb2312"); // Using GB2312 to explain this pile of bytes, you can assemble it as a normal string // Like Return MSG; */}/ ** * Connect ADSL * grammar: Rasdial connection name Username Password * Examples: Rasdial My Broadband HZHz1234567890 DFDFDFDF */ Public Static Boolean DSL (String Adsltital, String Adslname, String Adslpass) Throws Exception {Systemm .out.println ("Establishing connection."); String adslcmd = "Rasdial" + Adsltital + " + AdslName +" " + Adslpass; String Tempcmd = Executecmd (ADSLCMD); // String tempcmd = Executecmd ("Ping WWW .Youku.com "); // Determine whether it is successful if (tempcmd.indexof (" connected ")> 0) {system.out.println (" The connection has been successfully established. "); .err.println (tempcmd); System.err.println ("Establishing a connection failure"); Return false;} / ** * disconnect ADSL * / Public Static Boolean Cutadsl (String Adslltitle) Exception {string cutadsl = " RasDial " + Adsltital +" /Disconnect "; String res) + "Connection does not exist!") ; Return false;} else {system.out.println ("connection has been disconnected"); Return true;}}}}Note: The above code fragment actually calls the Rasdial command of Windows. So the main credit is Rasdial, Java is just a shell. Save the country again. Do you have a better way? If you have a better way, please tell me,^_^