-新建工具类
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class IPAddressQuerier
{
public static String queryIPAddress(String ipStr)
{
String res="";
int ind1=0;
int ind2=0;
try
{
String urlStr = "http://www.ip138.com/ips138.asp?ip="+ipStr+"&action=2";
HttpClient httpclient = new HttpClient();
GetMethod get = new GetMethod(urlStr);
httpclient.executeMethod(get);
int code = get.getStatusCode();
System.out.println("response status:"+code);
if (code == HttpStatus.SC_OK)
{
res = new String(get.getResponseBody(),"gb2312");
ind1 = res.indexOf("本站主数据:");
ind2 = res.indexOf("<",ind1);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return res.substring(ind1+"本站主数据:".length(),ind2);
}
public static void main(String[] args)
{
IPAddressQuerier.queryIPAddress("ip地址");
}
}