IP动态变化功能java如何实现

Python032

IP动态变化功能java如何实现,第1张

ava代码

语法:rasdial 连接名称 /disconnect

实例: rasdial 宽带 /disconnect

java程序调用rasdial命令

Java代码

package com.sesame.network

import java.io.BufferedReader

import java.io.InputStreamReader

public class ConnectNetWork {

/**

* 执行CMD命令,并返回String字符串

*/

public static String executeCmd(String strCmd) throws Exception {

Process p = Runtime.getRuntime().exec(“cmd /c ” + strCmd)

StringBuilder sbCmd = new StringBuilder()

BufferedReader br = new BufferedReader(new InputStreamReader(p

.getInputStream()))

String line

while ((line = br.readLine()) != null) {

sbCmd.append(line + ”\n”)

}

return sbCmd.toString()

}

/**

* 连接ADSL

*/

public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {

System.out.println(“正在建立连接.”)

String adslCmd = ”rasdial ” + adslTitle + ” ” + adslName + ” ”

+ adslPass

String tempCmd = executeCmd(adslCmd)

// 判断是否连接成功

if (tempCmd.indexOf(“已连接”) >0) {

System.out.println(“已成功建立连接.”)

return true

} else {

System.err.println(tempCmd)

System.err.println(“建立连接失败”)

return false

}

}

package com.tan.test

import java.io.BufferedInputStream

import java.io.IOException

import java.net.URL

import java.net.URLConnection

import org.apache.log4j.Logger

public class TestProxyIp {

private static final Logger log = Logger.getLogger(TestProxyIp.class)

public static void main(String[] args) throws IOException {

System.setProperty("http.maxRedirects", "50")

System.getProperties().setProperty("proxySet", "true")

// 如果不设置,只要代理IP和代理端口正确,此项不设置也可以

String ip = "59.175.192.126"

ip = "221.214.180.130"

ip = "122.224.171.91"

ip = "58.221.213.166"

ip = "202.106.16.36"

ip = "121.8.191.34"

ip = "222.208.242.30"

ip = "219.239.90.85"

ip = "60.31.177.188"

System.getProperties().setProperty("http.proxyHost", ip)

System.getProperties().setProperty("http.proxyPort", "3128")

//确定代理是否设置成功

log.info(getHtml("http://www.ip.cn/"))

//log.info(getHtml("http://20140507.ip138.com/ic.asp"))

}

private static String getHtml(String address){

StringBuffer html = new StringBuffer()

String result = null

try{

URL url = new URL(address)

URLConnection conn = url.openConnection()

conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatibleMSIE 7.0NT 5.1GTB5.NET CLR 2.0.50727CIBA)")

BufferedInputStream in = new BufferedInputStream(conn.getInputStream())

try{

String inputLine

byte[] buf = new byte[4096]

int bytesRead = 0

while (bytesRead >= 0) {

inputLine = new String(buf, 0, bytesRead, "UTF-8")

/*if (!"".equals(inputLine)) {

System.out.println(inputLine)

}*/

html.append(inputLine)

bytesRead = in.read(buf)

inputLine = null

}

buf = null

}finally{

in.close()

conn = null

url = null

}

//result = new String(html.toString().trim().getBytes("ISO-8859-1"), "UTF-8").toLowerCase()

//result=new String(html.toString().trim().getBytes("ISO-8859-1"), "GBK")

}catch (Exception e) {

e.printStackTrace()

return null

}/*finally{

html = null

}*/

return html.toString()

}

}

但是找不到有用的动态ip。