简体   繁体   English

错误时如何从Apache httpclient获取html内容?

[英]How do you get html content from Apache httpclient on error?

我在Java中使用Apache http客户端,但注意到它拒绝在非200个结果上获取内容...如何覆盖它?

I've tried this: 我已经试过了:

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.*;

public class HttpClientTutorial {

  private static String url = "http://www.apache.org/";

  public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
            new DefaultHttpMethodRetryHandler(3, false));

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      System.out.println(new String(responseBody));

    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    }  
  }
}

on the 200 and 404 and it works as expected (error code + content). 在200和404上运行,并且按预期方式工作(错误代码+内容)。 The code is from apache website itself: http://hc.apache.org/httpclient-3.x/tutorial.html 该代码来自apache网站本身: http : //hc.apache.org/httpclient-3.x/tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在Java中使用Apache HttpClient进行GET时出错 - Error while trying to do GET using Apache HttpClient in Java Apache httpclient GET响应实体内容为空 - Apache httpclient GET response entity content is empty 如何从Servlet中的HttpResponse获取内容? - How do you get content from a HttpResponse in servlet? Apache HttpClient 不使用分页链接获取页面内容。 我得到 200 状态但 html 没有内容 - Apache HttpClient doesn't get content of pages using pagination links. I get 200 status but html has no content 如何使用Java在Excel电子表格中获取单元格的内容? Apache POI - How do you get a cell's content in an Excel spreadsheet using Java? Apache POI 您如何通过 apache 的 commons httpclient 使用 NTLM 身份验证以编程方式对 Web 服务器进行身份验证? - How do you programatically authenticate to a web server using NTLM Authentication with apache's commons httpclient? apache HttpClient,基于表单的登录,并检索HTML内容 - apache HttpClient, form-based login, and retrieve HTML content 如何使用Apache httpclient获得自定义的Content-Disposition行? - How can I get a custom Content-Disposition line using Apache httpclient? 来自本地文件系统的Ap​​ache httpclient GET文件? - Apache httpclient GET file from local filesystem? 如何使用apache httpClient API? - How do I use the apache httpClient API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM