HTTPUrlConnection 为 Linux 返回 400 但适用于 Windows
HTTPUrlConnection is returning 400 for Linux but works on Windows
我正在使用以下代码请求抽搐api
URL url = new URL("https://api.twitch.tv/kraken/streams/?channel=" + channelID)
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("Accept", "application/vnd.twitchtv.v5+json");
connection.addRequestProperty("Client-ID", clientID);
connection.setUseCaches(false);
connection.setDoInput(true);
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = br.readLine()) != null) {
response.append(line);
response.append("\r");
}
br.close();
return response.toString();
这在 windows 上得到了正确的响应,但是当我 运行 它在我的 Raspberry PI 上得到了 400 响应。我找到了 我尝试使用此代码设置代理设置
System.setProperty("java.net.useSystemProxies", "true");
然而,这未能解决此请求,知道是什么原因造成的吗?
为了防止将来有人遇到这个问题,我升级到 java 11 并使用 http client in java 11 实现了它 我完全不知道为什么这会解决问题,但确实如此。
我正在使用以下代码请求抽搐api
URL url = new URL("https://api.twitch.tv/kraken/streams/?channel=" + channelID)
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("Accept", "application/vnd.twitchtv.v5+json");
connection.addRequestProperty("Client-ID", clientID);
connection.setUseCaches(false);
connection.setDoInput(true);
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = br.readLine()) != null) {
response.append(line);
response.append("\r");
}
br.close();
return response.toString();
这在 windows 上得到了正确的响应,但是当我 运行 它在我的 Raspberry PI 上得到了 400 响应。我找到了
System.setProperty("java.net.useSystemProxies", "true");
然而,这未能解决此请求,知道是什么原因造成的吗?
为了防止将来有人遇到这个问题,我升级到 java 11 并使用 http client in java 11 实现了它 我完全不知道为什么这会解决问题,但确实如此。