All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liuchink.chinkutils.baidu.ChinkBaiDuMapIpUtils Maven / Gradle / Ivy

There is a newer version: 0.0.14.RELEASE
Show newest version
package com.liuchink.chinkutils.baidu;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.liuchink.chinkutils.baidu.bean.ip.IpReq;
import com.liuchink.chinkutils.baidu.bean.ip.IpResp;
import com.liuchink.chinkutils.json.ChinkFastJsonUtils;
import com.liuchink.chinkutils.net.ChinkOkHttpUtils;
import com.liuchink.chinkutils.net.bean.HttpResponse;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author liugang
 * @since 2022/2/9 15:48
 */
@Slf4j
public class ChinkBaiDuMapIpUtils {

	private ChinkBaiDuMapIpUtils() {

	}

	/**
	 * 地理编码解析
	 *
	 * @param ipReq
	 *            ipReq
	 * @return com.liuchink.chinkutils.baidu.map.bean.BaiDuMapResp
	 * @author liugang
	 * @since 2020/10/27 15:49
	 */
	public static IpResp ip(IpReq ipReq) throws NoSuchAlgorithmException,
			IOException, KeyManagementException {
		StringBuilder url = new StringBuilder(
				"http://api.map.baidu.com/location/ip?");
		for (Map.Entry stringEntry : JSON
				.parseObject(ChinkFastJsonUtils.toJsonString(ipReq))
				.entrySet()) {
			if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
				url.append(stringEntry.getKey()).append("=")
						.append(stringEntry.getValue()).append("&");
			}
		}
		String postUrl = CharSequenceUtil.removeSuffix(url, "&");
		log.debug(":请求报文:【{}】", postUrl);
		HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
		IpResp ipResp = new IpResp();
		log.debug(":应答报文:【{}】",
				httpResponse.getResult().getBody());
		if (httpResponse.getStatus().getCode() == 200) {
			ipResp = JSON.parseObject(httpResponse.getResult().getBody(),
					new TypeReference() {
					});
		} else {
			ipResp.setStatus(httpResponse.getStatus().getCode());
			ipResp.setMessage("网络请求错误");
		}
		return ipResp;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy