I can receive the response. How can I get the response in a XML document? do I need to use an external XML parser? thanks for any helps
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);
if (statusCode == 200 ){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
log.info("\n" + content);
}else {
log.warn("failed to response");
}
Apache HttpClient - Http Get Request 1 Step 1 - Create a HttpClient object. The createDefault () method of the HttpClients class returns a CloseableHttpClient object, which is the base implementation of the HttpClient interface. 2 Step 2 - Create an HttpGet Object. ... 3 Step 3 - Execute the Get Request. ... 4 Example. ... 5 Output. ...
Follow the steps given below to execute the request using a response handler. The createDefault () method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface. Using this method create an HttpClient object
The createDefault () method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface. Using this method create an HttpClient object Instantiate the response handler object created above using the following line of code −
This page will walk through Apache HttpClient get example. Apache HttpGet class processes the request URI with HTTP GET method and returns response in the form of an entity. | Sign In Ask Question ConcretePage.com HOME ALL TUTORIALS JAVA 8 SPRING BOOT ANGULAR ANDROID Home > Apache API Apache HttpClient Get By Arvind Rai, October 15, 2018
I got my answer, post here for people have the same question
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With