[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2545
  • Last Modified:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect

I try to run the following code.

import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class Deneme
{
  public static void main(String[] args)
  throws Exception
  {
    String httpsURL = "https://registration.boun.edu.tr/";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    con.setConnectTimeout(10000000);
    InputStream ins = con.getInputStream();
    InputStreamReader isr=new InputStreamReader(ins);
    BufferedReader in =new BufferedReader(isr);
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
  }
}

But it returns following exception message:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.Socket.connect(Socket.java:507)
      at sun.net.NetworkClient.doConnect(NetworkClient.java:152)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
      at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:278)
      at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:335)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:176)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:744)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:162)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:913)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
      at testClient.Deneme.main(Deneme.java:16)
0
canist
Asked:
canist
2 Solutions
 
colr__Commented:
You are connecting to the server and waiting for a response without actually asking for anything.

For example, your coneting to https://registration.boun.edu.tr/, but you ned to write to the connetion to request a particular document, which you arent doing at the moment.

Try changing the URL to include the filename your trying to donwload.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> con.setConnectTimeout(10000000);

Should perhaps be done before the call to openConnection ()
0
 
mindwalkrCommented:
Are you in a corporate behind a firewall or something ?

Seems to me that you to specify  a proxy.
Take a look here: http://www.javaworld.com/javaworld/javatips/jw-javatip42.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KantiCommented:
Is there a method  setConnectTimeout(10000000);  in HttpsURLConnection
0
 
colr__Commented:
Some input from the poster would be helful.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Is there a method  setConnectTimeout(10000000);  in HttpsURLConnection

Its there in Java 5, in URLConnection itself:

http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#setConnectTimeout(int)

I was wrong in saying that it should be called before the openConnection () because before openConnection () you won't have the handle 'con' initialized.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now