try {
String ipprovince;
URL url = new URL("http://whois.pconline.com.cn/ip.jsp?ip=58.214.5.162");
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "代理服务器域名或ip地址");
System.setProperty("http.proxyPort", "8000");
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
String authentication = "用户名:密码";
String encodedLogin = new BASE64Encoder()。encodeBuffer(authentication.getBytes()); // 1
encodedLogin = encodedLogin.replace(" ", ""); // 2
connect.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
connect.connect();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
byte[] b = outStream.toByteArray();
// 关闭
outStream.close();
is.close();
connect.disconnect();
String address = new String(b, "gb2312"); // 3
if (address.indexOf("省") != -1) {
ipprovince = address.substring(0, address.indexOf("省") + 1);
System.out.println("省地址为:" + ipprovince);
} else if (address.indexOf("区") != -1) {
ipprovince = address.substring(0, address.indexOf("区") + 1);
System.out.println("省地址为:" + ipprovince);
} else {
ipprovince =
address.substring(0, address.indexOf("市") + 1);
System.out.println("省地址为:" + ipprovince);
}
} catch (Exception e) {
e.printStackTrace();
}
1. 使用eclipse开发,代码下面出现红线有错误,无法实行编译和执行。于是,在导入库类时“The type **** is not accessible due to restriction on required library”的错误出现。
顾名思义,错误意思为是所需要的类库由于受限制无法访问。
解决方法:
a) 选中项目--右键--进入Properties(属性)视图
b) 选中Java Build Path--点击Libraries--展开JRE System Library[JavaSE-1.6],选中Access rules这一项(如果没有,那就是JDK安装和配置的问题)。
c) Edit--点击Add--在Rule Pattern(规则式样)编辑你允许导入的类库,如(org/**),允许就是在Resolution选项中选中Accessible(当然,有些项目需要可以选择Forbidden、Discourage某些类库)
d) OK…
2. 这里有个JDK的BUG.
3. 如果输出有乱码,要转换一下