【问题标题】:Java HTTP Response Code, URL, IOExceptionJava HTTP 响应代码、URL、IOException
【发布时间】:2011-02-09 15:02:57
【问题描述】:

我正在尝试与之通信的服务器收到 503 响应。现在,它在日志中向我显示了这一点,但是,我想捕获它触发的 IOException 并在且仅当响应代码为 503 时才处理它,仅此而已。我该怎么做?

编辑:

这是我的部分代码:

inURL = new BufferedReader(new InputStreamReader(myURL.openStream()));

String str;
while ((str = inURL.readLine()) != null) {
    writeTo.write(str + "\n");
}

inURL.close();

【问题讨论】:

  • 您使用的是什么 HTTP 客户端 API?
  • 你能发布一些你的代码吗?
  • 查看我的编辑。 Balus:包裹在 InputStreamReader 周围的 URL,包裹在 bufferedreader 周围。

标签: java http networking url network-programming


【解决方案1】:

如果使用java.net.HttpURLConnection,则使用方法getResponseCode()

如果使用org.apache.commons.httpclient.HttpClientexecuteMethod(...)返回响应码

【讨论】:

    【解决方案2】:

    如果 IOEXception catch 子句属于该代码部分,请处理您想要的情况。

    检查http状态码是否为503,按照你的方式处理,如果不是再次抛出异常。

    【讨论】:

      【解决方案3】:

      这是我做的一个 HTTPClient :

      public class HTTPClient {
      
          /**
           * Request the HTTP page.
           * @param uri the URI to request.
           * @return the HTTP page in a String.
           */
          public String request(String uri){
      
              // Create a new HTTPClient.
              HttpClient client = new HttpClient();
              // Set the parameters
              client.getParams().setParameter("http.useragent", "Test Client");
              //5000ms
              client.getParams().setParameter("http.connection.timeout",new Integer(5000));
      
              GetMethod method  = new GetMethod();
      
              try{
                  method.setURI(new URI(uri, true));
                  int returnCode = client.executeMethod(method);
      
                  // The Get method failed.
                  if(returnCode != HttpStatus.SC_OK){
                      System.err.println("Unable to fetch default page," +
                              " status code : " + returnCode);
                  }
      
                  InputStream in = method.getResponseBodyAsStream();
      
                  // The input isn't null.
                  if (in != null) {
                      StringBuilder sb = new StringBuilder();
                      String line;
      
                      try {
                          BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                          while ((line = reader.readLine()) != null) {
                              sb.append(line).append("\n");
                          }
                      } finally {
                          in.close();
                      }
                      //return statement n0
                      return sb.toString();
                         } 
                  else {  
                      //return statement n1
                      return null;
                  }
      
              } catch (HttpException he) {
                  System.err.println("    HTTP failure");
              } catch (IOException ie) {
                  System.err.println("    I/O problem");
              } finally {
                method.releaseConnection();
              }
              //return statement n2
              return null;        
          }
      }
      

      也许它可以帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        • 2020-11-12
        相关资源
        最近更新 更多