postman和浏览器访问正常,java代码http请求访问出现403和404

当用postman和浏览器访问的时候,能够正常获取数据,在代码调用接口的时候出现403和404错误。

一、出现问题的原因

根据访问的网站的管理员的提示,对访问的参数进行调整,模拟浏览器访问。
在这里插入图片描述

二、模拟浏览器访问

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class holidayTest {
	public static void main(String[] args) {
		String methodUrl = "http://timor.tech/api/holiday/year/2021/";
		HttpURLConnection connection = null;
		BufferedReader reader = null;
		String line = null;
		try {
			URL url = new URL(methodUrl);
			// 根据URL生成HttpURLConnection
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");// 默认GET请求
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            //模拟postman
			connection.setRequestProperty("User-Agent", "PostmanRuntime/7.26.8");
			
			connection.connect();// 建立TCP连接
			int type = connection.getResponseCode();
			if (type == HttpURLConnection.HTTP_OK) {
				// 发送http请求
				reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
				StringBuilder result = new StringBuilder();
				// 循环读取流
				while ((line = reader.readLine()) != null) {
					result.append(line).append(System.getProperty("line.separator"));// "\n"
				}
				System.out.println(result.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			connection.disconnect();
		}
	
	}
}

三、以下代码出现:403

以下代码需要模拟浏览器的访问才能解决问题。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @author ldg
 * 访问节假日接口
 */
public class holidayTest {
	public static void main(String[] args) {
		String methodUrl = "http://timor.tech/api/holiday/year/2021/";
		HttpURLConnection connection = null;
		BufferedReader reader = null;
		String line = null;
		try {
			URL url = new URL(methodUrl);
			// 根据URL生成HttpURLConnection
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");// 默认GET请求
			connection.connect();// 建立TCP连接
			
			int type = connection.getResponseCode();
			if (type == HttpURLConnection.HTTP_OK) {
				// 发送http请求
				reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
				StringBuilder result = new StringBuilder();
				// 循环读取流
				while ((line = reader.readLine()) != null) {
					result.append(line).append(System.getProperty("line.separator"));// "\n"
				}
				System.out.println(result.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			connection.disconnect();
		}
	}
}

参考资料

【免费的节假日API】http://timor.tech/api/holiday
【模拟浏览器请求访问】https://www.cnblogs.com/wyw-action/articles/6682620.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java接口调用浏览器出现响应乱码,但在Postman中不乱码的情况下,可能有几个原因。首先,Java中的RestTemplate默认使用的是ISO-8859-1编码格式,而浏览器一般使用UTF-8编码格式,这导致了字符编码不一致,从而出现了响应乱码的问题。解决这个问题的方法是将RestTemplate中的messageConverters列表中的StringHttpMessageConverter的编码格式设置为UTF-8,以确保响应信息能够正确解析成UTF-8格式的字符串。 另外,如果请求头中包含accept-encoding:gzip,返回的结果是经过压缩的,而RestTemplate默认不会自动解压。解决这个问题的方法有两种:一种是在Java发起请求时将请求头中的accept-encoding:gzip去掉,另一种是让RestTemplate能够解压gzip格式的响应数据。 综上所述,要解决Java接口在浏览器出现响应乱码而Postman中不乱码的问题,可以通过将RestTemplate中的StringHttpMessageConverter的编码格式设置为UTF-8,并确保请求头中不包含accept-encoding:gzip来解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java发送Http请求响应结果乱码、Postman请求结果正常](https://blog.csdn.net/dreambyday/article/details/130303732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [RestTemplate发请求收到响应乱码,Postman正常](https://blog.csdn.net/qq_34972627/article/details/122685966)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值