162.Java中 发出请求获取别人的数据(阿里云 查询IP归属地)

1.效果

调用阿里云的接口 去定位IP地址

d55e0359a9fc6a5ffd20ffbec5f9fb0705b.jpg

2. 代码

/**
     * 1. Java中远程调用方法
     *             http://localhost:8080/mavenssm20180519/invokingUrl.action
     * @Title: invokingUrl
     * @Description: 
     * @return void
     * @throws Exception 
     * @throws 
       @date 2018年7月22日 下午11:58:58
     */
    @RequestMapping("/invokingUrl.action")
    public void invokingUrl(HttpServletRequest request) throws Exception{
        
        //------------------------java中发送请求---------------开始------------
        //记录登录着的id信息
        //(1)拿到用户的ip
        String remoteAddr = request.getRemoteAddr();
        //(2) 通过淘宝IP地址库获取IP位置
        // 动态调用数据库数据
        PrintWriter out = null;
        BufferedReader in =null;
        String getData ="";
        String line;
        URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip="+remoteAddr);//import java.net.URL; 
        URLConnection conn = url.openConnection();// import java.net.URLConnection;
        //发生post必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
        //获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            out.flush();
        //定义  BufferedReader 输入流的URL响应
            in =  new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            while((line = in.readLine())!=null){
                getData += line;
            }
            

            
        System.out.println(getData);
        //------------------------java中发送请求---------------结束------------
    }

2. 在java发送get/post的请求

2.1 utils 类

package cn.mg.kindeditor.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class sendRequestUtils {
    /**
     * 1.post方式发送请求
     * @param requestUrl
     * @return
     * @throws Exception
     */
        public  static String sendPostRequest(String requestUrl) throws Exception{
    
             //------------------------java中发送请求---------------开始------------
            //记录登录着的id信息
            //(1)拿到用户的ip
            //(2) 通过淘宝IP地址库获取IP位置
            // 动态调用数据库数据
            PrintWriter out = null;
            BufferedReader in =null;
            String getData ="";
            String line;
            URL url = new URL(requestUrl);//import java.net.URL; 
            URLConnection conn = url.openConnection();// import java.net.URLConnection;
            //发生post必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
            //获取URLConnection对象对应的输出流
                out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
                out.flush();
            //定义  BufferedReader 输入流的URL响应
                in =  new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
                while((line = in.readLine())!=null){
                    getData += line;
                }
                
            System.out.println(getData);
             
            return getData;
        }
        
        /**
         * 2.get方式发送请求
         * @param url
         * @return
         */
        public static String sendGetRequest(String url){
            String result = "";
            String line;
            StringBuffer sb=new StringBuffer();
            BufferedReader in = null;
            try {
             
                URL realUrl = new URL(url);
                // 打开和URL之间的连接
                URLConnection conn = realUrl.openConnection();
                // 设置通用的请求属性 设置请求格式
                conn.setRequestProperty("contentType", "utf-8"); 
                conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                //设置超时时间
                conn.setConnectTimeout(60);
                conn.setReadTimeout(60);
                // 建立实际的连接
                conn.connect();
                // 定义 BufferedReader输入流来读取URL的响应,设置接收格式
                in = new BufferedReader(new InputStreamReader(
                        conn.getInputStream(),"utf-8"));
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                result=sb.toString();
            } catch (Exception e) {
                System.out.println("发送GET请求出现异常!" + e);
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return result;
        }
}
 

 

2.2  调用(应用)

2.2.1 post

0ddbfb267f875302c54e979211e3fc72944.jpg

效果:

59c9b342da6073d580d26a815389a232863.jpg

 

2.2.2 get

167791db79571e2e1503eededbf8bf3c86b.jpg

效果:

775a1855351b148ac0b132eef49c3a41232.jpg

3. 附件

码云:

https://gitee.com/Luck_Me/javaToGetOrPost/tree/master

百度云:

链接:https://pan.baidu.com/s/1FJSngPT72DwNTrgsWV-cLg 
提取码:7tv2 

 

转载于:https://my.oschina.net/springMVCAndspring/blog/1861120

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ,我们可以使用第三方库来获取 IP归属地信息。其一个常用的库是 GeoIP2,它基于 MaxMind 的 GeoIP2 数据库。 首先,需要将 GeoIP2 库添加到项目。可以在 Maven 或 Gradle 构建脚本添加相应的依赖项。 然后,我们可以使用 GeoIP2 提供的 API 来查询 IP归属地。以下是一个示例代码: ```java import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.record.Country; import java.io.File; import java.io.IOException; import java.net.InetAddress; public class IPUtils { public static String getIPCountry(String ip) { try { File database = new File("GeoIP2-City.mmdb"); DatabaseReader reader = new DatabaseReader.Builder(database).build(); InetAddress ipAddress = InetAddress.getByName(ip); CityResponse response = reader.city(ipAddress); Country country = response.getCountry(); return country.getName(); } catch (IOException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String ip = "123.456.789.0"; String country = getIPCountry(ip); System.out.println("IP " + ip + " 的归属地是:" + country); } } ``` 在上述示例,我们通过 `getIPCountry` 方法传入一个 IP 地址,并在 `main` 方法调用该方法来获取IP归属地信息。具体的归属地信息包括国家、地区、城市等,可以根据需要进行扩展和处理。 需要注意的是,在运行代码之前,我们需要下载并导入 GeoIP2 数据库文件 `GeoIP2-City.mmdb`,该文件包含了 IP 地址和归属地信息的映射关系。可以从 MaxMind 的官网或其他数据获取该文件。 这样,我们就可以通过 Java获取 IP归属地信息了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值