doulan2827 2017-02-07 16:09
浏览 191

使Java报告为IPv6地址而不是IPv4

For a project in Java, i use a users ip address as a unique identifier for a website. It all works fine when a user only has a IPv4 address.

But here's my problem, when a user has a IPv6 address, Java will report a IPv4 address, while their browser will report a IPv6 address (In the $_SERVER['REMOTE_ADDR'] call in PHP).

So how do i make Java report it's IPv6 address insted of IPv4 ?

Edit: Some clarification:

This is only used to allow one person to download a file. It does not matter if several people use the same PC, not really important.

Say a user goes to http://whatismyipaddress.com/ with chrome, it will display a IPv6 address.

But if i now use Java to fetch http://whatismyipaddress.com/ it will show a IPv4 address. Why is this and how can i make it show the IPv6 address shown in Chrome ?

Edit 2: The code i use

public static String get(String url) throws WebFetchException {
    String result = "";
    try {
            URL url1 = new URL(url);
            HttpsURLConnection urlConn = (HttpsURLConnection) url1.openConnection();
            urlConn.setConnectTimeout(8 * 1000);
            urlConn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String text;
            while ((text = in.readLine()) != null) {
                result = result + text;
            }
            in.close();
        }
    } catch (Exception e) {
        throw new WebFetchException("There was an exception while fetching the requested page: " + url);
    }
    return result;
}
  • 写回答

1条回答 默认 最新

  • douba3943 2017-02-07 16:57
    关注

    The assumption that an ip address can be used as an identifier is wrong. More and more ISPs are deploying some form of NAT so many users will share a pool of IPv4 assesses. The address that a user is coming from can change from one connection to the other, so you can't rely on it being stable.

    On the other hand more users also get IPv6, and devices have multiple IPv6 addresses that will change over time.

    It also happens that because of connectivity issues users will switch between IPv4 and IPv6. Well, the user won't even notice, their device will just do it.

    So relying on ip addresses just won't work.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错