c0ding4what
c0ding4what

Reputation: 21

Fetch source code of web page using java?

I have a URL like this and the following method

public static void saveContent( String webURL )throws Exception 
{

    URL website = new URL(webURL);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        response.append(inputLine);

    in.close();

    System.out.println(response.toString());
}

However, When I want to print web content, it always fetches the source code of the main page(www.google.com).

How can I solve my problem ? Thanks for your help.

Upvotes: 0

Views: 206

Answers (1)

m0tylan0ga
m0tylan0ga

Reputation: 94

I copied yours code to netbeans and it seems to work correctly. I think the problem could lead on content in method argument "webURL". Try run your app on debug mode and look what you've got back there.

Upvotes: 1

Related Questions