Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response as XML Document with Apache Httpclient api?

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");
 }
like image 796
androidkc Avatar asked Nov 18 '10 15:11

androidkc


People also ask

How do I make a GET request from Apache httpclient?

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. ...

How to execute a httpclient request using a response handler?

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

How to create httpclient object using httpclients createdefault () method?

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 −

What is httpget in Apache API?

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


1 Answers

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();
            }                           
        }
like image 123
androidkc Avatar answered Oct 14 '22 20:10

androidkc