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 ]