@Component
public class Test {
public static void main(String[] args) {
URL url = null;
URLConnection conn = null;
long start = System.currentTimeMillis();
long end = 0L;
try {
url = new URL("http://www.baidu.com");
conn = url.openConnection();
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000);
InputStream openStream = null;
openStream = conn.getInputStream();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(openStream));
String tmp = null;
while (true) {
try {
if (!((tmp = reader.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
builder.append(tmp + "\n");
}
end = System.currentTimeMillis();
System.out.println(end - start);
} catch (Exception e) {
System.out.println("-------------------------------");
System.out.println(16);
}
}
}
conn.setConnectTimeout(15000);
conn.setReadTimeout(15000); //15000为设置的毫秒数