?
Solved

Opening a Web page from a java application

Posted on 2004-08-10
9
Medium Priority
?
336 Views
Last Modified: 2010-03-31
Hi!

I am looking for some example java code, which will allow me to connect to a specific webserver, and return the page as a string, which I can use in my java application..

Many Thanks,

Chris
0
Comment
Question by:chrishughes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 200 total points
ID: 11761110
like this: ?

---------------------

import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class HtmlPanel extends JPanel implements HyperlinkListener {
    protected JEditorPane html;

    public HtmlPanel(String startURL) {
        setLayout(new BorderLayout());

        try {
            URL url = new URL(startURL);
            html    = new JEditorPane(url);
            html.setEditable(false);
            html.addHyperlinkListener(this);
            JScrollPane scroller = new JScrollPane();
            JViewport vp = scroller.getViewport();
            vp.add(html);
            vp.setBackingStoreEnabled(true);
            add(scroller, BorderLayout.CENTER);
 
        } catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e);
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }

    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            System.out.println("myMess"+e.getURL());
            linkActivated(e.getURL());
        }
    }

    protected void linkActivated(URL u) {
        Cursor c = html.getCursor();
        Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
        html.setCursor(waitCursor);
        SwingUtilities.invokeLater(new PageLoader(u, c));
    }

    class PageLoader implements Runnable {
        URL url;
        Cursor cursor;

        PageLoader(URL u, Cursor c) {
            url = u;
            cursor = c;
        }
        public void run() {
            if (url == null) {
                html.setCursor(cursor);
                Container parent = html.getParent();
                parent.repaint();
            } else {
                Document doc = html.getDocument();
                try {
                    html.setPage(url);
                } catch (IOException ioe) {
                    html.setDocument(doc);
                    getToolkit().beep();
                } finally {
                    url = null;
                    SwingUtilities.invokeLater(this);
                }
            }
        }
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("Roadster");
        HtmlPanel panel = new HtmlPanel(args[0]);

        frame.getContentPane().add(panel);
        frame.setSize(500, 300);
        frame.setVisible(true);
    }
}
0
 

Author Comment

by:chrishughes
ID: 11761201
Kind of - But I am not looking for any form of graphical solution. I just want to return the page as a text string to my command prompt based application.
0
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 11761215
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Assisted Solution

by:alikoank
alikoank earned 100 total points
ID: 11761255
 public String readURL(String url) {
    StringBuffer buf = new StringBuffer();
    String s=null;
    try {
      URL u = new URL(url);
      BufferedReader in = new BufferedReader(
          new InputStreamReader(u.openStream()));
      while ( (s = in.readLine()) != null) {
        buf.append(s);
      }
    } catch (MalformedURLException ex) {
    } catch (IOException ex) {
    }
    return buf.toString();
  }
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 200 total points
ID: 11761258
http://javaalmanac.com/egs/java.net/ReadFromURL.html

------------

try {
        // Create a URL for the desired page
        URL url = new URL("http://hostname:80/index.html");
   
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println( str ) ;
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
0
 

Author Comment

by:chrishughes
ID: 11761301
That looks like the solution - however which packages do I need to import, to get it to work?

Thanks
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 200 total points
ID: 11761315
java.net.*;
java.io.*;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11761367
:-)

Good luck!

Tim
0
 
LVL 35

Expert Comment

by:girionis
ID: 11761376
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question