因为http协议是被动的,所以只能在服务器端设置超时。如果你要在客户端设置超时属性的话,我觉得应该用socket类来编程。不知我说得是否正确。
gz

解决方案 »

  1.   

    1。把链接要求的操作放到一个线程里2。在另外一个线程里(或者什么地方都行)记时3。如果超时,把1的那个线程干掉。(interupt)
      

  2.   

    写得有点赶,没有测试,如果有问题明天再说吧,现在有事,不好意思了。刚刚考虑好像也可以用listener这种来实现,没时间多想了,你先看看吧:)import  java.io.*;  
    import  java.util.*;  
     
    public  class  ThreadTest  extends  Thread  
    {  public boolean flag = true;
       public static ThreadTest t;
       public  void  run()  {  
       try  {  
        HandlEvent h = new HandlEvent();
    h.start();
    sleep(3*1000);
    if( flag ){
    h.interrupt();
    System.out.println("cao shi");
    }   
       }  catch(Exception  e)  {  
       }  
       }  
                 
       public  static  void  main(String[]  args)  {    
       
       t  =  new  ThreadTest();   
       t.start();
                                                     
       }
       
       class HandlEvent extends Thread{
       public void run(){
       try  { 
      boolean ret  =  false;//youclass.testurl();
      sleep(2*1000);
      if( ret ){
    t.flag = false;
    System.out.println("do somthing");
      }
       }  catch(Exception  e){  
       }   
       }
       }
    }
      

  3.   

    用线程控制,同意http是被动说法
    自己做线程监控可行方案
      

  4.   

    package task;import java.net.*;
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class Test {
      public boolean testUrl(java.net.URL url){
        class TestThread extends Thread{
          public java.net.URL url;
          public  Thread parent ;
          public  boolean flag=false;
          public TestThread(java.net.URL url){
            this.url=url;
          }
          public void run(){
            try{
              System.out.println(new java.util.Date(System.currentTimeMillis()));
              HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
              httpConnection.getURL();
              int responseCode=httpConnection.getResponseCode();
              if(responseCode==200){
                flag= true;
                parent.notify();
              }
            }catch(java.io.IOException e){
              e.printStackTrace();
            }
          }
        }
        TestThread tt=new TestThread(url);
        tt.start();
        try{
          Thread.currentThread().sleep(3000);
        }catch(Exception e){
          e.printStackTrace();
        }
        tt.stop();
        System.out.println(new java.util.Date(System.currentTimeMillis()));
        System.out.println("flag:"+tt.flag);
        return tt.flag;  }
      public static void main(String[] args){
        Test t=new Test();
        try{
          t.testUrl(new java.net.URL("http://www.sina.com.cn/"));
        }catch(Exception e){
          e.printStackTrace();
        }
      }
    }