• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.net.ConnectException

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code works fine for me:


Can you open the URL in a browser?
 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Joe
i opened that url in a browser,it is working fine.
[ February 16, 2006: Message edited by: mohan kumar rayapaneni ]
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic