Exception while getting response from the URL

Hi,

here is my program for URL:

URL url = new URL("http://google.com");

System.out.println(url);

BufferedReader in = new BufferedReader(

new InputStreamReader(

url.openStream()));

String inputLine;

StringBuffer sb = new StringBuffer();

while ((inputLine = in.readLine()) != null)

sb.append(inputLine);

FileOutputStream fos = new FileOutputStream(new File("myout.xml"));

fos.write(sb.toString().getBytes());

fos.close();

in.close();

i succesfully executed the program but its working only with intranet URL's ...... like, if i have any web server running and if i give an URL of tht webserver, its working fine.... but

when i used google.com as url its not working, and it thorows exception

java.net.ConnectException: Connection timed out: 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:452)

at java.net.Socket.connect(Socket.java:402)

at sun.net.NetworkClient.doConnect(NetworkClient.java:139)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:402)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:618)

at sun.net.www.http.HttpClient.<init>(HttpClient.java:306)

at sun.net.www.http.HttpClient.<init>(HttpClient.java:267)

at sun.net.www.http.HttpClient.New(HttpClient.java:339)

at sun.net.www.http.HttpClient.New(HttpClient.java:320)

at sun.net.www.http.HttpClient.New(HttpClient.java:315)

at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:521)

at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:498)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:626)

at java.net.URL.openStream(URL.java:913)

at temp.UrlExample.main(UrlExample.java:19)

Exception in thread "main"

and i connect to internet using proxy. is that and issue for this exception?

Thnks,

NG

[2280 byte] By [JavaBaby@79a] at [2007-11-27 1:56:37]
# 1
As you are behind the proxy server.You have to connect with the proxy server and ask it to get this url for you.So now you have to communicate with the proxy server instead of directly connecting with the internet site/domain.
jawadhashmia at 2007-7-12 1:31:13 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your response,As you said to connect to the proxy server, is there any API availabe in java.net package?
JavaBaby@79a at 2007-7-12 1:31:13 > top of Java-index,Java Essentials,Java Programming...
# 3
no ideayou have to check the RFC (Request for Comments) for this.Also check might be there is API for using behind Proxy Server.
jawadhashmia at 2007-7-12 1:31:13 > top of Java-index,Java Essentials,Java Programming...
# 4

Java supports for connection behind the proxy.

Check this one

try {

URL url = new URL("http://www.google.com");

InetSocketAddress isa = new InetSocketAddress("192.168.0.12", 8080);

Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);

URLConnection urlConnection = url.openConnection(proxy);

urlConnection.connect();

InputStream inStream = urlConnection.getInputStream();

BufferedInputStream binStream = new BufferedInputStream(inStream);

int i = 0;

byte []b = new byte[128];

File file = new File("google.htm");

FileOutputStream fout = new FileOutputStream(file);

while(i != -1){

i = binStream.read(b);

if(i > 0)

fout.write(b);

System.out.print(b);

}

fout.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

jawadhashmia at 2007-7-12 1:31:13 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank you
JavaBaby@79a at 2007-7-12 1:31:13 > top of Java-index,Java Essentials,Java Programming...