Reading a file in the internet

Whats wrong with this one ? I compiled it and there was no error but
why is it not displaying the file in the dos prompt?

import java.net.*;
import java.io.*;

public class NetFile {

      public static void main(String args[]) {
            
            try {

                  String line;
                      StringBuffer buf = new StringBuffer();

                  String url = "http://mysite.com/Sample.txt";
                  URL path = new URL(url);

                  URLConnection conn = path.openConnection();
                        conn.connect();

                        BufferedReader data = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                        while ((line = data.readLine()) != null) {
                          buf.append(line + "\n");
                        }

                        System.out.println(buf.toString());
                        data.close();
            } catch(Exception e) {
            }
      }
}
simple21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aozarovCommented:
The program looks fine to me (I changed the URL and runned sucessfuly).
Does http://mysite.com/Sample.txt exist?
can you print the exception:
change:
 } catch(Exception e) {
          }

to:
 } catch(Exception e) {
e.printStackTrace();
          }
0
aozarovCommented:
check what happens when you use your browser to get http://mysite.com/Sample.txt
0
simple21Author Commented:
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Met
        at java.net.PlainSocketImpl.doConnect(Unknown Source
        at java.net.PlainSocketImpl.connectToAddress(Unknown
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)

It seems there was a problem in the file that I'm accessing or it can be because I'm behind a firewall?
Give me a link to your file and I'll try to read it thanks.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

simple21Author Commented:
> Does http://mysite.com/Sample.txt exist?
That is just a dummy site my site really has the file.
0
aozarovCommented:
0
aozarovCommented:
>> This is just a dummy site
I assumed so. did you try to request that URL using your browser?
0
simple21Author Commented:
> I assumed so. did you try to request that URL using your browser?

Yes and its working so do you think because I'm behind a firewall ?
0
CodingExpertsCommented:
try {
      InputStream in=url.openStream ();
      BufferedReader dis =
        new BufferedReader (new InputStreamReader (in));
      fBuf = new StringBuffer  () ;

      while ( (line = dis.readLine ()) != null) {
        fBuf.append (line + "\n");
      }

      in.close ();
    }

-CE
0
aozarovCommented:
CodingExperts, why do you think url.openStream() is going to change anything?
>> Yes and its working so do you think because I'm behind a firewall ?
So, I assume your program was working for you with google URL but what about requesting the URL
from your browser on the same machine that you run the program?
firewall might be one reason, but then the browser should fail as well.
0
aozarovCommented:
is your site using port 80 or 8080?
0
simple21Author Commented:
> So, I assume your program was working for you with google URL but what about requesting the URL
> from your browser on the same machine that you run the program?

Everything works fine w/ my browser but when I try to run my codes its giving this :

 java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Met
        at java.net.PlainSocketImpl.doConnect(Unknown Source
        at java.net.PlainSocketImpl.connectToAddress(Unknown
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)

>  is your site using port 80 or 8080?
The website i'm trying to access is myjavaserver.com its using port : 21
0
aozarovCommented:
You are aware that port 21 is used normally by FTP...
But if this is your case then try http://myjavaserver.com:21/Sample.txt
That is strange that putting http://myjavaserver.com/Sample.txt worked as is in your web browser.
0
CodingExpertsCommented:
You can use the httpunit to do the required, wherein you can connect thru a proxy server.

-CE
0
CodingExpertsCommented:
0
aozarovCommented:
BTW, does your browser have proxy settings enabled?
If so, then you will need to set them for you java app (check this link for the settings: http://www.davidreilly.com/java/java_network_programming/#2.4)
0
simple21Author Commented:
I'm actually at work the site and file that I'm trying to access is on I can actually see it but when I run the codes
as I said its giving the error so what do you think about it?
0
simple21Author Commented:
> BTW, does your browser have proxy settings enabled?
> If so, then you will need to set them for you java app (check this link for the settings:

Yes I have a proxy enabled and how will I add this to my codes ?

Properties sysProperties = System.getProperties();

// Specify proxy settings
sysProperties.put("proxyHost", "myhost");
sysProperties.put("proxyPort", "myport");
sysProperties.put("proxySet",  "true");
0
aozarovCommented:
Right. before you do anything else
0
aozarovCommented:
or as mentioned using the jvm command line options:
java -DproxySet=true -DproxyHost=myhost -DproxyPort=myport NetFile
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simple21Author Commented:
Haha that works cool ;)
0
aozarovCommented:
Nice :-)
0
simple21Author Commented:
So its really something about work related proxies ey :D
0
aozarovCommented:
Yep, you had a good hunch about it ;-)
0
simple21Author Commented:
Thanks for the help I really appriciate both of you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.