Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CloseableHttpClient抛出java.net.SocketTimeoutException:读取超时_Java_Get_Httpclient - Fatal编程技术网

CloseableHttpClient抛出java.net.SocketTimeoutException:读取超时

CloseableHttpClient抛出java.net.SocketTimeoutException:读取超时,java,get,httpclient,Java,Get,Httpclient,我尝试使用get请求从NSE-India检索选项链数据: 当邮递员客户端/chrome浏览器请求时,响应在650毫秒后返回 我试图从ApacheHTTP客户端检索相同的数据 public static void main(String args[]) { int timeout = 3; RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000)

我尝试使用get请求从NSE-India检索选项链数据:

当邮递员客户端/chrome浏览器请求时,响应在650毫秒后返回

我试图从ApacheHTTP客户端检索相同的数据

public static void main(String args[]) {
        int timeout = 3;
        RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000)
                .setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        try {
//          HttpGet getRequest = new HttpGet("https://reqres.in/api/users?page=2");
            HttpGet getRequest = new HttpGet("https://www.nseindia.com/api/option-chain-equities?symbol=INFY");
            getRequest.addHeader("accept", "*/*");
            HttpResponse response = httpClient.execute(getRequest);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new RuntimeException("Failed with HTTP error code : " + statusCode);
            }
            HttpEntity httpEntity = response.getEntity();
            String output = EntityUtils.toString(httpEntity);

            System.out.println(output);
            JAXBContext jaxbContext = JAXBContext.newInstance(Records.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Records records = (Records) jaxbUnmarshaller.unmarshal(new StringReader(output));
            System.out.println(records);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
并抛出java.net.SocketTimeoutException:Read timed out 但我相信设定的时间足以获取数据。 我尝试了另一个get请求

它工作得很好

需要一些关于如何调试此场景的建议

这是堆栈跟踪:

好的,看来HTTPS连接成功了,您已经成功发送了请求

一种可能的解释是,nseindia.com故意“慢行”你的请求。这是一些网站用来阻止不必要请求的策略;e、 g.违反其使用条款的自动请求。这个想法是他们采取。。。A.非常长的时间响应请求,以使提交请求的自动化要么被完全阻止,要么被严重限制


另一种可能性是,您的网络基础设施中存在一个问题,即“黑洞”从nseindia.com返回的一些网络数据包。(您没有说明您是否在运行web浏览器的同一台计算机上运行Java代码。或者您的web浏览器是否正在通过代理。)

更改代码以打印stacktrace并向我们显示。把它加到问题上。我们需要知道异常被抛出的位置。。。在你的代码中,在你调用的库代码中。谢谢你的提示,我尝试设置1。“Referer”,但它不起作用。2.“用户代理”——它起作用了。addHeader(“用户代理”、“Mozilla/5.0”);如果你不得不这么做,你很有可能违反了他们的使用条款。你可能会发现他们开始对你采取其他更严厉的行动。。。比如阻止你的IP地址。可能是,但是,所有可用的数据都是由网站公开的。而我改变了(“用户代理”,“Karthik”);“可能是,但是,所有可用的数据都是由网站公开的。”-该论点不会被法庭接受。。。除非你有个好律师。甚至在那时也不可能。该数据根据公布的使用条款向公众提供。如果您忽略了ToU,您可能违反了法律。谢谢您的关注。我将彻底检查他们的网站使用条款和条件一次。