Hi,
I am pretty familar with the Java programming lanuguage, and have written code to read the get the html code of general URLs. However, if the URL takes the form of a google search result (ie) : 4
then my approach fails. The URL is accessible from inside an IE or Netscape browser. Does anybody have any suggestion to how I can accesss these types of URLs using the Java programming lanugage.
BTW, this is a sample of my existing code.
// Broswer.java
// this program uses a JEditorPane to display the contents
// of a file on a web server.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class Browser extends JFrame {
private JTextField enter;
private JEditorPane contents;
public Browser(String firstLink)
{
super("Web Browser"
;
Container c=getContentPane();
JPanel upperPanel=new JPanel();
upperPanel.setLayout(new java.awt.GridLayout(2,1));
enter=new JTextField(firstLink);
final String firstPage=firstLink;
enter.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getThePage(e.getActionCommand());
}
}
);
upperPanel.add(enter);
contents=new JEditorPane();
contents.setEditable(false);
contents.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e)
{
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
getThePage(e.getURL().toString());
}
}
);
JScrollPane contentsScrollPane=new JScrollPane(contents);
contentsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
contentsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentsScrollPane.setPreferredSize(new Dimension (500,500));
contentsScrollPane.setMinimumSize(new Dimension (480,480));
c.add(contentsScrollPane,BorderLayout.CENTER);
//set up a JButton to go Back from
JButton finishButton=new JButton("Start Page"
;
finishButton.setToolTipText("Click to go Back to the First Page in Browser"
;
finishButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enter.setText(firstPage);
getThePage(firstPage); //ie .load the initial page
}
}
);
upperPanel.add(finishButton);
c.add(upperPanel,BorderLayout.NORTH);
//now set up the initial page.
getThePage(firstLink); //ie .load the initial page
//add a windowlistener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
//must close the streams -done before here
setVisible(false);
dispose();
}
}
);
setSize(700,500);
// pack();
show();
}
private void getThePage(String location)
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
contents.setPage(location);
enter.setText(location);
}
catch (IOException io) {
JOptionPane.showMessageDialog(this,"Error retrieving specified URL",
"Bad URL", JOptionPane.ERROR_MESSAGE);
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
I am pretty familar with the Java programming lanuguage, and have written code to read the get the html code of general URLs. However, if the URL takes the form of a google search result (ie) : 4
then my approach fails. The URL is accessible from inside an IE or Netscape browser. Does anybody have any suggestion to how I can accesss these types of URLs using the Java programming lanugage.
BTW, this is a sample of my existing code.
// Broswer.java
// this program uses a JEditorPane to display the contents
// of a file on a web server.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class Browser extends JFrame {
private JTextField enter;
private JEditorPane contents;
public Browser(String firstLink)
{
super("Web Browser"
Container c=getContentPane();
JPanel upperPanel=new JPanel();
upperPanel.setLayout(new java.awt.GridLayout(2,1));
enter=new JTextField(firstLink);
final String firstPage=firstLink;
enter.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getThePage(e.getActionCommand());
}
}
);
upperPanel.add(enter);
contents=new JEditorPane();
contents.setEditable(false);
contents.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e)
{
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
getThePage(e.getURL().toString());
}
}
);
JScrollPane contentsScrollPane=new JScrollPane(contents);
contentsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
contentsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentsScrollPane.setPreferredSize(new Dimension (500,500));
contentsScrollPane.setMinimumSize(new Dimension (480,480));
c.add(contentsScrollPane,BorderLayout.CENTER);
//set up a JButton to go Back from
JButton finishButton=new JButton("Start Page"
finishButton.setToolTipText("Click to go Back to the First Page in Browser"
finishButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enter.setText(firstPage);
getThePage(firstPage); //ie .load the initial page
}
}
);
upperPanel.add(finishButton);
c.add(upperPanel,BorderLayout.NORTH);
//now set up the initial page.
getThePage(firstLink); //ie .load the initial page
//add a windowlistener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
//must close the streams -done before here
setVisible(false);
dispose();
}
}
);
setSize(700,500);
// pack();
show();
}
private void getThePage(String location)
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
contents.setPage(location);
enter.setText(location);
}
catch (IOException io) {
JOptionPane.showMessageDialog(this,"Error retrieving specified URL",
"Bad URL", JOptionPane.ERROR_MESSAGE);
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}