V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wzx155
V2EX  ›  Steam

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

  •  
  •   wzx155 · 2021-03-12 12:14:33 +08:00 · 2168 次点击
    这是一个创建于 1351 天前的主题,其中的信息可能已经有所发展或是发生改变。
    9 条回复    2022-06-30 16:50:36 +08:00
    lu5je0
        1
    lu5je0  
       2021-03-12 12:18:49 +08:00 via Android
    steam 有现成的 api 。多看看文档
    rabbirbot00
        2
    rabbirbot00  
       2021-03-12 15:46:21 +08:00
    如果你的连返回的 JSON 数据都拿不到报超时的话,是因为 steamcommunity.com 是 Steam 社区的地址,在国内是没法直接访问的,之前我做毕设的时候就遇到了这个问题,如果你要在 Java 项目中访问只能走代理分流或者用国外服务器反代。
    wzx155
        3
    wzx155  
    OP
       2021-03-12 16:25:51 +08:00
    @rabbirbot00
    为什么我开 fq 软件后浏览器可以并收到返回的获取数据,但是在 java 项目中连接超时呢?
    Boyizmen
        4
    Boyizmen  
       2021-03-12 17:05:03 +08:00
    #3 你的 java client 也需要设置 proxy 的
    rabbirbot00
        5
    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 包在本机跑也不会有问题。
    wzx155
        7
    wzx155  
    OP
       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
        8
    rabbirbot00  
       2021-03-13 22:04:40 +08:00
    @wzx155 老代码也找不到了不记得当时怎么写的了,没有响应码的话没法确定是哪里的问题,但是一般来说都是 Steam 强制 HTTP 跳转到 HTTPS 了,拿到响应代码看看呢
    ql562482472
        9
    ql562482472  
       2022-06-30 16:50:36 +08:00
    你的 proxy 不对呗 指不定是 socks5 被你当成 http 的调了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3627 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:19 · PVG 18:19 · LAX 02:19 · JFK 05:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.