有大佬能帮忙看看 Java 怎么获取这个网址返回的数据吗? https://steamcommunity.com/inventory/76561198426206369/730/2?l=chinese&count=5000

2021-03-12 12:14:33 +08:00
 wzx155
2173 次点击
所在节点    Steam
9 条回复
lu5je0
2021-03-12 12:18:49 +08:00
steam 有现成的 api 。多看看文档
rabbirbot00
2021-03-12 15:46:21 +08:00
如果你的连返回的 JSON 数据都拿不到报超时的话,是因为 steamcommunity.com 是 Steam 社区的地址,在国内是没法直接访问的,之前我做毕设的时候就遇到了这个问题,如果你要在 Java 项目中访问只能走代理分流或者用国外服务器反代。
wzx155
2021-03-12 16:25:51 +08:00
@rabbirbot00
为什么我开 fq 软件后浏览器可以并收到返回的获取数据,但是在 java 项目中连接超时呢?
Boyizmen
2021-03-12 17:05:03 +08:00
#3 你的 java client 也需要设置 proxy 的
rabbirbot00
2021-03-12 17:43:32 +08:00
@wzx155 楼上的方法我不清楚具体做法,我当时是确定本地代理在哪个端口上,比方说如果在 1080 端口的话,就在访问接口的那段 Java 代码里加上代理信息 {"http:":"127.0.0.1:1080", "https:":"127.0.0.1:1080"} 这样最后打出的 JAR 包在本机跑也不会有问题。
kingwrcy
2021-03-12 18:00:00 +08:00
java 设置系统变量 http 代理,搜搜就有了
wzx155
2021-03-12 18:37:34 +08:00
@rabbirbot00

大佬我买了个国外的 ip 设置了代理,连接到是不超时了,现在又 Connection reset 了

package com.example.demo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@SpringBootTest
class DemoApplicationTests {
@Test
public void testGet1() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String urlStr = "https://steamcommunity.com/profiles/76561198426206369/inventory/json/730/2";
HttpGet httpGet = new HttpGet(urlStr);
httpGet.addHeader("User-Agent" ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45");
ResponseHandler responseHandler;
String ip = "150.109.186.75";
int port = **;
HttpHost proxy = new HttpHost(ip, port);
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute( httpGet);
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(s);
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (closeableHttpClient != null) {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


2021-03-12 18:29:08.185 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {tls}->http://150.109.186.75:**->https://steamcommunity.com:443: Connection reset
2021-03-12 18:29:08.185 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : Retrying request to {tls}->http://150.109.186.75:6396->https://steamcommunity.com:443
2021-03-12 18:29:08.280 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {tls}->http://150.109.186.75:**->https://steamcommunity.com:443: Connection reset
2021-03-12 18:29:08.280 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : Retrying request to {tls}->http://150.109.186.75:6396->https://steamcommunity.com:443
2021-03-12 18:29:08.382 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {tls}->http://150.109.186.75:**->https://steamcommunity.com:443: Connection reset
2021-03-12 18:29:08.382 INFO 17188 --- [ main] o.apache.http.impl.execchain.RetryExec : Retrying request to {tls}->http://150.109.186.75:6396->https://steamcommunity.com:443

java.net.SocketException: Connection reset
rabbirbot00
2021-03-13 22:04:40 +08:00
@wzx155 老代码也找不到了不记得当时怎么写的了,没有响应码的话没法确定是哪里的问题,但是一般来说都是 Steam 强制 HTTP 跳转到 HTTPS 了,拿到响应代码看看呢
ql562482472
2022-06-30 16:50:36 +08:00
你的 proxy 不对呗 指不定是 socks5 被你当成 http 的调了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/761001

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX