需要先购买商品
开源免费的!!!!
需要依赖
<dependency> <groupId>com.baidubce</groupId> <artifactId>api-explorer-sdk</artifactId> <version>1.0.0</version> </dependency>
代码如下:
工具类:
import com.baidubce.http.ApiExplorerClient; import com.baidubce.http.AppSigner; import com.baidubce.http.HttpMethodName; import com.baidubce.model.ApiExplorerRequest; import com.baidubce.model.ApiExplorerResponse; import com.baidubce.model.ApiExplorerResponseMetadata; public class Iputils { public static String getCityInformation(String ip, String AccessKey, String AppSecret) { String path = "http://ipapi.api.bdymkt.com/ip2location/retrieve"; ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path); request.setCredentials(AccessKey, AppSecret); request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8"); String requestExample = "{\n \"ip\": \"" + ip + "\"\n}"; request.setJsonBody(requestExample); ApiExplorerClient client = new ApiExplorerClient(new AppSigner()); try { ApiExplorerResponse response = client.sendRequest(request); // 返回结果格式为Json字符串 String result = response.getResult(); return result; } catch (Exception e) { e.printStackTrace(); } return "获取失败!!!!!!!"; } }
测试:
import com.alibaba.fastjson.JSON; import com.financial.sys.domain.ipDto; import com.financial.sys.utils.Iputils; public class getip { public static void main(String[] args) { String city = Iputils.getCityInformation("请输入ip地址", "你的AccessKey", "你的AppSecret"); ipDto ipDto = JSON.parseObject(city, ipDto.class); System.out.println("城市是:"+ipDto.getCountry()); System.out.println("省是:"+ipDto.getProvince()); System.out.println("城市是:"+ipDto.getCity()); System.out.println("县是:"+ipDto.getCounty()); System.out.println("提供商是:"+ipDto.getIsp()); } }
自己封装实体类!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119915.html