Reputation: 559
I am trying to access the url using below mentioned code
public static void main(String[] args) throws Exception {
//CookieHandler.setDefault(new CookieManager(null,CookiePolicy.ACCEPT_ALL));
// http://www.google.com/
// http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g
**URL oracle = new URL("http://finance.yahoo.com/d/quotes.csv?s=XOM+EK+JNJ+MSFT&f=snd1t1l1ohgvwdyr");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));**
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
This code is working fine in my home but same code is not working in office and i am getting error like this.
Exception in thread "main" java.net.ConnectException: Connection timed out: 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.SocksSocketImpl.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.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.yahoo.Connection.main(Connection.java:21)
I am suspecting there can be firewall or proxy issue but i am not sure.
Does anyone know of way to work around this?
Thanks.
Upvotes: 1
Views: 5612
Reputation: 2327
You could try setting these properties for your program. With them java should be able to use your proxy. That is, as long as you have the information on hand.
System.setProperty("http.proxyHost", "your proxyhost");
System.setProperty("http.proxyPort", "your proxyport");
Upvotes: 2
Reputation: 11337
You're probably behind some corporate proxy.
If yes try this code to enable programmatically a proxy:
private boolean proxyEnabled = true;
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://finance.yahoo.com/d/quotes.csv?s=XOM+EK+JNJ+MSFT&f=snd1t1l1ohgvwdyr");
URLConnection yc;
if(proxyEnabled) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("my.proxy.example.com", 3128));
yc = oracle.openConnection(proxy);
} else {
yc = oracle.openConnection();
}
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
Upvotes: 2