Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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
Java 从URL读取文件时出现问题-超时_Java_Amazon Web Services_Amazon Ec2 - Fatal编程技术网

Java 从URL读取文件时出现问题-超时

Java 从URL读取文件时出现问题-超时,java,amazon-web-services,amazon-ec2,Java,Amazon Web Services,Amazon Ec2,我能够在本地执行下面的示例代码(输出:200OK),但无法在AWSEC2服务器中执行相同的代码(输出:超时)。直到上周,这段代码在AWSEC2中一直工作得很好,但从最近几天起就停止了工作 public class CheckURL { public static void main(String[] args) { HttpClient httpClient = HttpClients.custom().setUserAgent("Java Agent").build(); St

我能够在本地执行下面的示例代码(输出:200OK),但无法在AWSEC2服务器中执行相同的代码(输出:超时)。直到上周,这段代码在AWSEC2中一直工作得很好,但从最近几天起就停止了工作

public class CheckURL {
public static void main(String[] args) {
    HttpClient httpClient = HttpClients.custom().setUserAgent("Java Agent").build();
    String urlString = "https://www.nseindia.com/content/historical/EQUITIES/2019/DEC/cm26DEC2019bhav.csv.zip";

    try {
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);

        System.out.println("Status code: " + response.getStatusLine().getStatusCode());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

注意:连接到Internet网关和入站/出站的ec2服务器都设置为所有流量。有趣的是,上面的代码在ec2服务器中可以很好地使用as URL。nseindia.com网站有什么问题吗

任何帮助都将不胜感激


堆栈跟踪:

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)

使用
curl
从命令行尝试此操作时也会超时:

$ curl -iv https://www.nseindia.com/content/historical/EQUITIES/2019/DEC/cm26DEC2019bhav.csv.zip
*   Trying 104.90.192.157:443...
* TCP_NODELAY set
* Connected to www.nseindia.com (104.90.192.157) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /opt/local/share/curl/curl-ca-bundle.crt
CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=IN; ST=Maharashtra; L=Mumbai; O=National Stock Exchange of India Ltd.; OU=IT; CN=www.nseindia.com
*  start date: Dec  6 00:00:00 2019 GMT
*  expire date: Dec 12 12:00:00 2020 GMT
*  subjectAltName: host "www.nseindia.com" matched cert's "www.nseindia.com"
*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=GeoTrust RSA CA 2018
*  SSL certificate verify ok.
> GET /content/historical/EQUITIES/2019/DEC/cm26DEC2019bhav.csv.zip HTTP/1.1
> Host: www.nseindia.com
> User-Agent: curl/7.67.0
> Accept: */*
>
* OpenSSL SSL_read: Operation timed out
* Closing connection 0

因此,您这边没有问题。

您可能需要增加HttpClient和服务器的超时值。您是否尝试过将SSHing发送到服务器并尝试从此服务器ping此主机?@TomazFernandes示例中指定的URL h可访问,并且zip已下载。表示机器可以通过http访问。@Susovon Pal,我试过你的代码,代码只给了我200。是的,但如果他不能从EC2服务器ping这个主机,则表示这是AWS配置问题,而不是java问题。。。