Java - Gateway 504尝试从公共HTML表读取时出错

时间:2018-02-20 20:56:56

标签: java apache http httpclient

我正在尝试使用HTML Java从外部网站获取HttpClient表,但即使我可以从浏览器中点击网址,我也会收到超时错误。源可以阻止外部请求吗?

[HTTP/1.1 504 Gateway Time-out 300034ms]

我可以从浏览器点击此网址,并且有适当的防火墙规则允许流量到网址。

http://www.pjm.com/pub/account/lmpgen/lmppost.html

代码

 HttpClient httpClient = new DefaultHttpClient();
 try {
    String inputLine;
    content = "";

    HttpGet httpGet;
    HttpResponse response;
    try {
      logger.info("Getting content of PJM page: " + urlString);
      httpGet = new HttpGet(urlString);
      response = httpClient.execute(httpGet);
      int responseCode = response.getStatusLine().getStatusCode();

      if (responseCode == 200) {
         try (BufferedReader in = new BufferedReader(new InputStreamReader(httpClient.execute(httpGet).getEntity().getContent()))) {
              while ((inputLine = in.readLine()) != null) {
                 content = content + inputLine;
               }
         } catch (IOException e) {
             throw new IOException("An error occurred while attemptiong to read from " + urlString, e);
         }
      } else {
         logger.error("Request failed for PJM with response Code : " + responseCode);
      }
 } catch (Exception e) {
     throw new MalformedURLException("An error occurred while creating the URL " + urlString);

 } finally {
    httpClient.getConnectionManager().shutdown();
 }

错误

   2018-02-20 19:28:20,008 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/].[jsp]] (ajp-0.0.0.0-8009-11) Servlet.service() for servlet jsp threw exception
    java.io.IOException: An error occurred while attemptiong to read from http://www.pjm.com/pub/account/lmpgen/lmppost.html
Caused by: java.net.SocketException: Connection timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)

1 个答案:

答案 0 :(得分:0)

我建议使用基于apache http的简化http-request。 这段代码可以使用:

private static final String URI_STRING = "http://www.pjm.com/pub/account/lmpgen/lmppost.html";

private static final HttpRequest<String> HTTP_REQUEST =
        HttpRequestBuilder.createGet(URI_STRING, String.class).build();

private void perform(){
  ResponseHandler<String> rh = HTTP_REQUEST.execute();

  if(rh.isSuccess()){
    String content = rh.get();
  }else{
    logger.error("Request failed for PJM with response Code : " + rh.getStatusCode());
  }
}