CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Posts
    3

    How to timeout a URL connection?

    Does anyone know how to set timeout for a URL connection?

    I wrote a small program to fetch content and date of a URL from remote site. However, it hangs on some URLs (I tried these URLs on netscape, they also hangs). I would like to know whether I can set a timeout period for each connection. If you have a better way than writing specific threads to do this, please let me know.

    Thanks in advance.
    --Changzhou

    P.S. The following is my code. I can always got "[JDT-DEBUG] connection opened", but sometimes "[JDT-DEBUG] got date" does not come out, and the program runs forever.



    try {
    URL url = new URL((String)sites.elementAt(siteIndex)+page.urlPath);
    connection = url.openConnection();
    // DEBUG-SECTION-BEGIN, BY Java Developement Template
    System.out.println("[JDT-DEBUG]"+"["+id+"] connection opened");
    // DEBUG-SECTION-END, BY Java Developement Template
    } catch (Exception x) {
    log("[ERROR] Openning "+page.urlPath+":"+x);
    return DOWNLOADING_ERROR;
    }
    try {
    // test if it is necessary to download from remote site
    long date = connection.getDate() ;
    // DEBUG-SECTION-BEGIN, BY Java Developement Template
    System.out.println("[JDT-DEBUG]"+"["+id+"] got date");





  2. #2
    Guest

    Re: How to timeout a URL connection?

    Your best bet is to use create a java.net.Socket connection. In version 1.1 and greater you can set the timeout value for a socket. If the socket times out then it will throw an Exception. Catch the exception and continue from there.

    Van Glass
    [email protected]


  3. #3
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: How to timeout a URL connection?

    If URL class is used, u can access sites from clients running behind proxy also. And u don't have to do extra coding for it.
    But Socket can't access sites from clients running behind proxy directly. For accessing those sites, u have to implement the protocol for communicating with the proxy server.

    Whether to go for Socket / URL depends on whether u want ppl behind proxy to access remote sites on internet or not. If using socket u want that, u will have to implement proxy client in ur application.

    - UnicMan
    http://members.tripod.com/unicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured