当前位置: 首页> 游戏> 手游 > Spring Boot发送http请求

Spring Boot发送http请求

时间:2025/7/13 9:04:40来源:https://blog.csdn.net/a983677218/article/details/141598691 浏览次数:0次

        因为项目要调用第三方网站接口获取信息(类似python爬虫),所以在后端项目中需要前端axios一样去构建请求获取信息。下面用简单的获取IP地址为例。

@Slf4j
public class IPTest {// IP地址查询public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";// 未知地址public static final String UNKNOWN = "未知地址";private String getRealAddressByIP(String ip) {String address = UNKNOWN;if (true) {try {String rspStr = sendGet(IP_URL, "ip=" + ip + "&json=true" ,"GBK");if (StrUtil.isEmpty(rspStr)) {return UNKNOWN;}JSONObject obj = JSONObject.parseObject(rspStr);String addr = obj.getString("addr");return String.format("%s" , addr);} catch (Exception e) {}}return address;}private String sendGet(String url, String param, String contentType) {StringBuilder result = new StringBuilder();BufferedReader in = null;try {String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);URLConnection connection = realUrl.openConnection();connection.setRequestProperty("accept" , "*/*");connection.setRequestProperty("connection" , "Keep-Alive");connection.setRequestProperty("User-Agent","Mozilla/4.0 compatible; MSIE 6.0; Windows NT 5.1;DigExt");connection.connect();in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));String line;while ((line = in.readLine()) != null) {result.append(line);}} catch (ConnectException e) {   } catch (SocketTimeoutException e) {} catch (IOException e) {} catch (Exception e) {} finally {try {if (in != null) {in.close();}} catch (Exception ex) {}}return result.toString();}public static void main(String[] args) {String ipaddr = getRealAddressByIP(你的ip);String address = ipaddr.equals("未知地址") ? "位置获取失败" : ipaddr;log.info(address);}
}

       有些网站会做反爬机制,所以在创建URL的时候,加上这句 connection.setRequestProperty("User-Agent","Mozilla/4.0 compatible; MSIE 6.0; Windows NT 5.1;DigExt");可以解决大部分问题

关键字:Spring Boot发送http请求

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: