aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes java.net.ConnectException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java.net.ConnectException" Watch "java.net.ConnectException" New topic
Author

java.net.ConnectException

mohan kumar r
Ranch Hand

Joined: Jan 19, 2006
Posts: 38
hi all
i want to display the xml document as it is that is available at url "http://slashdot.org/slashdot.xml". i wrote two java classes to achieve this ( i am not showing package and import statements)
-----------------
URLGrabber.java
----------------
public class URLGrabber {
public static InputStream getDocumentAsInputStream(URL url)
throws IOException
{
InputStream in=url.openStream();
return in;
}
public static InputStream getDocumentAsInputStream(String url)
throws MalformedURLException,IOException{
URL u=new URL(url);
return getDocumentAsInputStream(u);
}
public static String getDocumentAsString(URL url) throws IOException{
StringBuffer result=new StringBuffer();
InputStream in=url.openStream();
int c;
while((c=in.read()) !=-1) result.append((char)c);
return result.toString();
}
public static String getDocumentAsString(String url)
throws MalformedURLException,IOException{
URL u=new URL(url);
return getDocumentAsString(u);
}

}
----------------
URLGrabberTest.java
-------------------
public class URLGrabberTest {
public static void main(String[] args) {
for(int i=0;i<args.length;i++){
try{
String doc=URLGrabber.getDocumentAsString(args[i]);
System.out.println(doc);
}catch(MalformedURLException e){
System.err.println("args[i] can not be interpreted as url");
}catch(IOException e){
e.printStackTrace();
}
}

}

}

-----
when i execute the second java class as

-------------------------------------
java URLGrabberTest http://www.slashdot.org/slashdot.xml
-----------------------------------
i am getting the exception stack trace as

-----------------------------------------
-------------------------------------------
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
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)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Sour
ce) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
at java.net.URL.openStream(Unknown Source)
at com.sns.net.URLGrabber.getDocumentAsString(URLGrabber.java:27)
at com.sns.net.URLGrabber.getDocumentAsString(URLGrabber.java:35)
at com.sns.net.URLGrabberTest.main(URLGrabberTest.java:21)
------------------------------------------------------------
--------------------------------------------------------------
[ February 16, 2006: Message edited by: mohan kumar rayapaneni ]

mohan
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Your code works fine for me:


Can you open the URL in a browser?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
mohan kumar r
Ranch Hand

Joined: Jan 19, 2006
Posts: 38
hi Joe
i opened that url in a browser,it is working fine.
[ February 16, 2006: Message edited by: mohan kumar rayapaneni ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.net.ConnectException