如果我将此代码用于特定的 url,那么它可以工作,但是如果我更改完全相同类型的 url,那么是否有必要更改getRequest.setHeader("User-Agent", "xxxx") (other than xxxx)
或者它不会导致任何问题?
DefaultHttpClient client = new DefaultHttpClient();
String getUrl = "http://myurl.com";
HttpUriRequest getRequest = new HttpGet(getUrl);
getRequest.setHeader("User-Agent", "xxxx");
HttpResponse response = client.execute(getRequest);
int statusCode = response.getStatusLine().getStatusCode();
log.info("statusCode=" + statusCode);
Document doc = null;
if (statusCode == 200 ){
HttpEntity entity = response.getEntity();
//String content = EntityUtils.toString(entity);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(entity.getContent());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}