Call a website page URL within Java code

Asked By 280 points N/A Posted on -
qa-featured

I need to call a remote website from my code and get the html data onto a string variable.

How do I do this using Java ?

SHARE
Best Answer by DimaZ
Answered By 5 points N/A #93009

Call a website page URL within Java code

qa-featured

Brandon, would you mind elaborating your requirement further ?

Details such as the Java version you are using, platform you are developing for (Desktop or Web), would help us in guiding you to the solution.

This is because solution depends on the context of the problem. In Java you need to be more specific.

Answered By 280 points N/A #93011

Call a website page URL within Java code

qa-featured

I am currently using JDK 6.0. I am implementing a e-commerce web application using Java and JSF.

The application server is Glassfish. My development environment is Netbeans.

The requirement is that I have to call an external page by passing some parameters. The destination page returns the values inform of an XML string. I need to process the results and update a database.

Answered By 5 points N/A #93012

Call a website page URL within Java code

qa-featured

Thank you for the information. It is possible to call an external page internally from Java code.

You create a IO Stream to the destination URL and then read the values to a string buffer.

It is similar to opening a file, the difference is that you give the URL as the path.

Answered By 280 points N/A #93013

Call a website page URL within Java code

qa-featured

Would it be possible to show me some sample code?

I tried the File object and passed the URL as a parameter.  It did not work.  Maybe I am importing the wrong libraries?

 

Best Answer
Best Answer
Answered By 0 points N/A #93014

Call a website page URL within Java code

qa-featured

When you  need to access a remote page , you actually need to use the "URL" object.

The following code will help you:

StringBuilder dataBuffer = new StringBuilder();    
try {
        BufferedReader in = new BufferedReader(new InputStreamReader(new URL(sPath).openStream()));
        String line ="";
        while ((line = in.readLine()) != null) {
          dataBuffer.append(line);
          line = "";
        }    
        in.close();
      }
        catch (Exception e) {
            log.error(e.getMessage(),e);
    }

Answered By 5 points N/A #93015

Call a website page URL within Java code

qa-featured

DimaZ beat me to posting the code 🙂

DimaZ's code is correct. What he is doing is that reading the web request chunk by chunk. This is because the Web server streams the file line by line. Therefore a buffer is filled as and when data becomes available.

You must bear in mind that the result "might" not be complete. For example if a packet loss happens the result string might not be a complete XML. Therefore you need to do pre-checks without taking for gratis that the code is fool proof 🙂

Answered By 280 points N/A #93016

Call a website page URL within Java code

qa-featured

Thank you DimaZ!

Your code worked beautifully. Thank you TekGirl for your advise!

Related Questions