[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Exception..

Hey all,
here's the stack trace:

java.net.ConnectionException: Conection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
      at java.net.Socket.connect(Socket.java:434)
      at java.net.Socket.connect(Socket.java:384)
      at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:386)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:602)
      at sun.net.www.http.HttpClient.(init)(HttpClient.java:303)
      at sun.net.www.http.HttpClient.(init)(HttpClient.java:264)
      at sun.net.www.http.HttpClient.New(HttpClient.java:336)
      at sun.net.www.http.HttpClient.New(HttpClient.java:317)
      at sun.net.www.http.HttpClient.New(HttpClient.java:312)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnection(HttpURLConnection.java:481)
      at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:472)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
      at MonitorTool.getText(MonitorTool.java:102)
      at MonitorTool.process(MonitorTool.java:68)
      at MonitorTool.(init)(MonitorTool.java:55)
      at MonitorTool.main(MonitorTool.java:79)

And here's the program code:

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.text.html.*;
import javax.swing.text.*;

public class MonitorTool extends JWindow implements ActionListener {
   
    Dimension dim         = Toolkit.getDefaultToolkit().getScreenSize();
    int       i_sWidth    = (int)dim.getWidth();
    int       i_sHeight   = (int)dim.getHeight();
   
    int       i_a_width   = 200;
    int       i_a_height  = 30;
   
    int       i_toolbarH  = 30;
   
    String    s_URL       = "http://www.positive-websolutions.co.uk/index.htm";
   
    Timer timer;
   
    JLabel    jl_status   = new JLabel();
    JButton   jb_exit     = new JButton();
   
    public MonitorTool () {
        super();
        setBounds( i_sWidth-i_a_width, i_sHeight-i_a_height-i_toolbarH, i_a_width, i_a_height );
       
        JPanel pane = new JPanel  (           );
       
        jl_status   = new JLabel  ( "Please wait");
        jb_exit     = new JButton ( "X"       );
        jb_exit.setSize           ( 15, 15    );
        jb_exit.setBorderPainted  ( false     );
        jb_exit.addActionListener ( this      );
       
        pane.add                  ( jb_exit   );
        pane.add                  ( jl_status );
       
        setContentPane            ( pane      );
       
        this.setVisible( true );
       
        int i_mins_delay = 4;           // delay for 4 mins.
       
        //i_mins_delay *= 60;           // calculate seconds equivalent.
        i_mins_delay *= 1000;           // calculate ms equivalent.
       
        for ( ;; ) { process ( i_mins_delay ); }
       
    }
   
    public void process ( int ip_delay ) {
       
        if ( ip_delay <= 0 ) System.exit(-1);
       
        try { Thread.sleep( ip_delay ); } catch (Exception e) {}
       
        jl_status.setText ( "Checking if page is up ..." );
       
        String s_web_data = null;
               s_web_data = getText ( s_URL );
       
        if ( s_web_data == null ) {
            jl_status.setText ( "Page is not up" );
        } else {
            jl_status.setText ( "Page is up!"    );
        }
       
    }
   
    public static void main ( String [] args ) {
        new MonitorTool();
    }
   
    public static String getText ( String uriStr ) {
        final StringBuffer buf = new StringBuffer(1000);
       
        try {
            // Create an HTML document that appends all text to buf
            HTMLDocument doc = new HTMLDocument() {
                public HTMLEditorKit.ParserCallback getReader(int pos) {
                    return new HTMLEditorKit.ParserCallback() {
                        // This method is whenever text is encountered in the HTML file
                        public void handleText(char[] data, int pos) {
                            buf.append(data);
                            buf.append('\n');
                        }
                    };
                }
            };
           
            // Create a reader on the HTML content
            URL url = new URI(uriStr).toURL();
            URLConnection conn = url.openConnection();
            Reader rd = new InputStreamReader(conn.getInputStream());
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            kit.read(rd, doc, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
       
        // Return the text
        return buf.toString();
    }
   
    public void actionPerformed ( ActionEvent evt ) {
        System.exit ( 0 );
    }
   
}

Can anyone spot the problem?
The exception says that it occurs on this line (mainly):
  Reader rd = new InputStreamReader(conn.getInputStream());

WHY?!

Thanks very much in advance! :D
[r.D]
0
DrWarezz
Asked:
DrWarezz
  • 11
  • 11
1 Solution
 
petmagdyCommented:
The connection is opened on my machine, do connect to internet using proxy?

try this copy the html file ( "http://www.positive-websolutions.co.uk/index.htm") to local directry and put it on ur local web server like apache and tomcat, and connect locally first
0
 
DrWarezzAuthor Commented:
hey petmagdy,

Hmm.. it returns the same exception, even when I attempt to load a page from: http://localhost/test.html

(note: it loads successfully via my browser [IE])..

?

ta,
[r.D]
0
 
petmagdyCommented:
try this DrWarezz:

            URLConnection conn = url.openConnection();
//            Reader rd = new InputStreamReader(conn.getInputStream());  //BufferedInputStream instead
            BufferedInputStream in =

                  new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            kit.read(in, doc, in.available());   //<---- notice the third parameter
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!

 
DrWarezzAuthor Commented:
nope.. same exception :o\  this is strange.

Thanks for the help so far,
[r.D]
0
 
petmagdyCommented:
this is working on my machine, is the exception trace changed?
can u please post ur changes?
0
 
DrWarezzAuthor Commented:
It's working on your machine? Okay (this is for a 'client', so hopefully it will work on his machine then), thus, although i still want to find out the problem, there's not as much urgency :-)

Okay, here's the code (with all the changes):


import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.html.*;
import javax.swing.text.*;

public class MonitorTool extends JWindow implements ActionListener {
   
    Dimension dim         = Toolkit.getDefaultToolkit().getScreenSize();
    int       i_sWidth    = (int)dim.getWidth();
    int       i_sHeight   = (int)dim.getHeight();
   
    int       i_a_width   = 170;
    int       i_a_height  = 30;
   
    int       i_toolbarH  = 30;
   
    String    s_URL       = "http://www.positive-websolutions.co.uk/index.htm";    // This is case-sensitive!
   
    JLabel    jl_status   = new JLabel();
    JButton   jb_exit     = new JButton();
   
    public MonitorTool () {
        super();
        setBounds( i_sWidth-i_a_width, i_sHeight-i_a_height-i_toolbarH, i_a_width, i_a_height );
       
        JPanel pane = new JPanel  (           );
       
        jl_status   = new JLabel  ( "Please wait");
        jb_exit     = new JButton ( "X"       );
        jb_exit.setSize           ( 15, 15    );
        jb_exit.setBorderPainted  ( false     );
        jb_exit.addActionListener ( this      );
       
        pane.add                  ( jb_exit   );
        pane.add                  ( jl_status );
       
        setContentPane            ( pane      );
       
        this.setVisible( true );
       
        int i_mins_delay = 4;           // delay for 4 mins.
       
        //i_mins_delay *= 60;             // calculate seconds equivalent.
        i_mins_delay *= 1000;           // calculate ms equivalent.
       
        for ( ;; ) { process ( i_mins_delay ); }
       
    }
   
    public void process ( int ip_delay ) {
       
        if ( ip_delay <= 0 ) System.exit(-1);
       
        try { Thread.sleep( ip_delay ); } catch (Exception e) {}
       
        jl_status.setText ( "Checking if page is up ..." );
       
        String s_web_data = null;
               s_web_data = getText ( s_URL );
       
        if ( s_web_data == null ) {
            jl_status.setText ( "Page is not up" );
        } else {
            jl_status.setText ( "Page is up!"    );
        }
       
    }
   
    public static void main ( String [] args ) {
        new MonitorTool();
    }
   
    public static String getText ( String uriStr ) {
        final StringBuffer buf = new StringBuffer(1000);
       
        try {
            // Create an HTML document that appends all text to buf
            HTMLDocument doc = new HTMLDocument() {
                public HTMLEditorKit.ParserCallback getReader(int pos) {
                    return new HTMLEditorKit.ParserCallback() {
                        // This method is whenever text is encountered in the HTML file
                        public void handleText(char[] data, int pos) {
                            buf.append(data);
                            buf.append('\n');
                        }
                    };
                }
            };
           
            // Create a reader on the HTML content
            URL url = new URI(uriStr).toURL();
            URLConnection conn = url.openConnection();
//            Reader rd = new InputStreamReader(conn.getInputStream());  //BufferedInputStream instead
            BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            kit.read(in, doc, in.available());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
       
        // Return the text
        return buf.toString();
    }
   
    public void actionPerformed ( ActionEvent evt ) {
        System.exit ( 0 );
    }
   
}


The exception doesn't seem to have changed, however, (due to the changes), it's pointing to this line:

BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );


ta,
[r.D]
0
 
petmagdyCommented:
The weired thing that on my machine it passed with the same code??!!

ok try this, create a new empty class with main method and just try this:

            URL url = new URI("The URL").toURL();
            URLConnection conn = url.openConnection();
           BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            kit.read(in, doc, in.available());

if still fails, try on a more simpler and shorter htmls file
0
 
DrWarezzAuthor Commented:
ie, use this?:

import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.util.*;

public class TestConn {

  public static void main ( String [] args ) {

      try {
            URL url = new URI("The URL").toURL();
            URLConnection conn = url.openConnection();
           BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            kit.read(in, doc, in.available());
      } catch (Exception e) {
          e.printStackTrace();
      }

  }

}


If so, it doesn't define the variable 'doc'...

ta, :-)
[r.D]
0
 
DrWarezzAuthor Commented:
*Obviously, I'd specify "The URL".  ;-)
0
 
petmagdyCommented:
ok just replace:

>>            kit.read(in, doc, in.available());
with:

System.out.println( in.available() );
0
 
DrWarezzAuthor Commented:
Okay, same exception, it's once again pointing to this line:

BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );

:'(

lol ta
[r.D]
0
 
petmagdyCommented:
ok I have no option but try with u as I can't replicate ur error on my machine, barrr with me :-)

change:
>>            URLConnection conn = url.openConnection();

to
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

by the way did u tried other html??
0
 
petmagdyCommented:
also one more thing, when it was like this:

>>            URLConnection conn = url.openConnection();
add this line after and post the result:
            System.out.println(conn.getClass().getName());
0
 
DrWarezzAuthor Commented:
Hey, sorry about the delay..

I tried this:

import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.util.*;

public class TestConn {

  public static void main ( String [] args ) {

      try {
            URL url = new URI("http://www.google.com/index.html").toURL();
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
           BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            System.out.println( in.available() );
      } catch (Exception e) {
          e.printStackTrace();
      }

  }

}

Same exception.
I then tried this:

import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.util.*;

public class TestConn {

  public static void main ( String [] args ) {

      try {
            URL url = new URI("http://www.google.com/index.html").toURL();
            URLConnection conn = url.openConnection();
      System.out.println(conn.getClass().getName());
           BufferedInputStream in = new BufferedInputStream( conn.getInputStream( ) );
           
           
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            System.out.println( in.available() );
      } catch (Exception e) {
          e.printStackTrace();
      }

  }

}

It outputted: sun.net.www.protocol.http.HttpURLConnection
..and then the exception

:o\
ta,
[r.D]
0
 
petmagdyCommented:
ok this only means one thing:

U have problem in ur enviroment or network, or u have a proxy, or gateway , does ur browser have a proxy?
0
 
DrWarezzAuthor Commented:
well, I'm running the internet via an ICS gateway (on my LAN).. however, we haven't made any changes, and I can still access the internet FINE via my browser..

Like I said above as well, I can't even access my own server via java (ie; http://localhost/page), whereas my browser can fine... :o\

Any ideas?
Thanks alot,
[r.D]
0
 
petmagdyCommented:
please try to run this code on local host after disabling ur gateway, remove the Gateway ip from ur TCP/IP configuration for this test

other thing did u ever modified ur java.security or java.policy
0
 
DrWarezzAuthor Commented:
Okay, I'll have a play about with everything.

>"did u ever modified ur java.security or java.policy"
Nope.

:o\

Thanks alot petmagdy.. I'm going to have a play around, then I've got to go out, so I'll get back to you over the weekend sometime :-)

Thanks alot for the help.. And have a good weekend.
[r.D]
0
 
petmagdyCommented:
thanks u have a good weekend too ;-)
0
 
ramazanyichCommented:
to check that it is not a problem of TCP/IP connection you can try conenct using telnet to port 80 of your webserver:
From command line just try:
cmd>telnet localhost 80
if connection is not opened then it is defenitly problem in your TCP/IP stack
0
 
DrWarezzAuthor Commented:
I haven't got round to trying anything out yet, but just to let you know, I haven't forgotten about this Q ... And I will get back to it soon :-)

Thanks,
[r.D]
0
 
DrWarezzAuthor Commented:
Okay, I'm back :-)

I was having other troubles with Java as well, and came to the conclusion that something was wrong :P hehe.
So, I reinstalled my J2SDK, and everything else, and it's ALL working great again! :D

However, I shall award you the points, petmagdy, because you went out of your way to try and solve this for me. :-)

Thanks alot!
[r.D]
0
 
petmagdyCommented:
thanks to u and welcome :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now