1
我在java中使用Apache http客戶端,但我注意到它拒絕在非200結果中獲取內容......我如何覆蓋它?如何從Apache httpclient錯誤獲取html內容?
我在java中使用Apache http客戶端,但我注意到它拒絕在非200結果中獲取內容......我如何覆蓋它?如何從Apache httpclient錯誤獲取html內容?
我已經試過這樣:
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();
}
}
}
在200和404
,它按預期工作(錯誤代碼+內容)。代碼來自apache網站本身:http://hc.apache.org/httpclient-3.x/tutorial.html
我建議去HttpClient 4.x. – 2012-02-03 14:16:30
非常沒有內容獲得非200返回碼。你確定你應該得到任何? – Mat 2012-02-03 13:22:00
100%肯定我需要它像捲曲一樣行事,返回錯誤的內容。 – MichaelICE 2012-02-03 13:23:59