用Socket把,可以设置Timeout时间

解决方案 »

  1.   

    URL url = new URL(“URL”);
                String host = url.getHost();
                int port = url.getPort();
                String path = url.getFile();
                if ("".equals(path)) {
                    path = "/";
                }
                String CR_LF = "\r\n";
                if (port == -1) {
                   port = 80; 
                }
                socket = new Socket(host, port);
                socket.setSoTimeout(SESSION_TIMEOUT);
                
                out = socket.getOutputStream();
                
                PrintWriter printOut = new PrintWriter(
                    new BufferedWriter(
                    new OutputStreamWriter(out, “MS932”)));
                
                String requestLine = new StringBuffer()
                    .append("POST ")
                    .append(path)
                    .append(" HTTP/1.0")
                    .append(CR_LF)
                    .toString();
                
                String requestHeader = new StringBuffer()
                    .append("Host: ").append(host)
                    .append(CR_LF)
                    .append("Content-length: ").append(requestData.length())
                    .append(CR_LF)
                    .append("Content-type: ")
                    .append("application/x-www-form-urlencoded")
                    .append(CR_LF)
                    .append("Prama: ").append("no-cache")
                    .append(CR_LF)
                    .toString();
                
                printOut.print(requestLine);
                printOut.print(requestHeader);
                printOut.print(CR_LF); // blank
                printOut.print(requestData);
                printOut.print(CR_LF);
                printOut.flush();            
                in = socket.getInputStream();
      

  2.   

    没用啊,一样的超时啊
    socket = new Socket(host, port);这句就超时了