为什么此URL会与apache客户端产生超时并使用URL连接?

时间:2016-11-03 13:52:07

标签: java url apache-commons-httpclient

这个简单的网址http://www.cert.fnmt.es/certs/ACCOMP.crt让我很头疼。您可以看到它可用,但我无法获得Apache HTTP客户端的内容

String url = "http://www.cert.fnmt.es/certs/ACCOMP.crt";

//Java URL openConnection OK
InputStream in = new URL(url).openConnection().getInputStream();
byte[] bytes = IOUtils.toByteArray(in);
System.out.println("OK? "+(bytes.length>0));

//Apache http client (SocketTimeoutException)
HttpGet httpGet = new HttpGet(url);     
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(httpGet);                
System.out.println("OK? "+(response != null));

此处引发了异常:client.execute(httpGet);

OK? true
Exception in thread "main" 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:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)
    at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)
    at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:153)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at test.CommonsDataLoader.main(CommonsDataLoader.java:102)

我正在使用Java 1.7和apache http-client4.3.4。我不知道这是一个错误还是与服务器不兼容。我的代码基于apache httpclient,我不能硬编码“特殊”网站的例外

1 个答案:

答案 0 :(得分:0)

请添加

 httpGet.addHeader("User-Agent", "Mozilla/5.0");

声音就像网站正在过滤用户代理。