技术中心

这里象征着我们的态度和能力

>Java通过代理访问外部网站
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-09      浏览次数:5117
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

    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. 如果输出有乱码,要转换一下

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730
Baidu
map