10
votes

Obtenir le contenu de la page de Apache Commons HTTP Demande

Donc, j'utilise Apache Commons HTTP pour apporter une demande à une page Web. Je ne peux pas pour la vie de moi comprendre comment obtenir le contenu réel de la page, je peux simplement obtenir des informations d'en-tête. Comment puis-je obtenir le contenu réel de celui-ci?

Voici mon exemple de code: xxx

merci!


0 commentaires

4 Réponses :


12
votes

Utilisez httpresponse # getentality () code> puis httpentity # getContent () code> pour l'obtenir sous forme d'introuvream code>.

InputStream input = response.getEntity().getContent();
// Read it the usual way.


0 commentaires

1
votes
response.getEntity();
You really want to look at the Javadocs, the example for HttpClient shows you how to get at all the info in the response: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html

0 commentaires

1
votes

Si vous voulez simplement le contenu de l'URL, vous pouvez utiliser l'API URL, comme ceci:

import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class URLTest {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.google.com.br");
        //here you have the input stream, so you can do whatever you want with it!
        Scanner in = new Scanner(url.openStream());
        in.nextLine();
    }
}


1 commentaires

J'aurais juste utilisé cela, mais j'avais besoin d'utiliser Apache Commons car c'est conjointement avec plus de choses.



17
votes

Le commentaire de Baluscs fonctionnera très bien. Si vous utilisez la version 4 ou plus récente d'Apache httpComponents, vous pouvez également utiliser une méthode de commodité: entitulétils.tostring (httpenty);

Voici ce qu'il ressemblera à votre code: xxx

J'espère que cela vous aide .

Je ne sais pas si cela est dû à des versions différentes, mais je devais la réécrire comme ceci: xxx


0 commentaires