String httpUrl = "https://www.baidu.com/s?ie=UTF-8&tn=90594569_hao_pg&wd=1";
URL url = null;
HttpURLConnection httpConn = null;
String result = "";
try {
String address = httpUrl;
url = new URL(address);
httpConn = (HttpURLConnection) url.openConnection();
//A URL connection can be used for input and/or output. Set the
//DoInput flag to true if you intend to use the URL connection for input,
//false if not. The default is true.
//URL连接可用于input或output。如果想用URL连接输入,设置DoInput标签为true。
//输入和输出是针对计算机的,如果以程序员的角度考虑,经常弄混。
//input输入,output输出,那么不是从output里read,input中write吗,其实相反
//input输入进计算机,计算机才能读,所以是从input read,而output是计算机输出,通过output write。
httpConn.setDoOutput(false);
//所以如果setDoInput(false),想从URLConnection读取时不能读取
//Cannot read from URLConnection if doInput=false (call setDoInput(true))
httpConn.setDoInput(true);
//连接建立超时时间还有读取数据超时时间,
httpConn.setConnectTimeout(600000);
httpConn.setReadTimeout(600000);
httpConn.setRequestMethod("GET");
httpConn.connect();
//获取状态码
int code = httpConn.getResponseCode();
System.out.println(code);
//读http请求响应
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
result = result + line+"\n";
}
System.out.println(result);
//关闭IO和连接
reader.close();
httpConn.disconnect();
}
catch(Exception e){
log.error(e);
}
finally{
if(httpConn!=null)
httpConn.disconnect();
}
catch(Exception e){
for (int i = 0; i < 6; i++) {
Thread t = new Thread(){public void run(){get();}};
t.start();
if(result.equals("ok")){
break;
}
try {
Thread.sleep(300000);
} catch (InterruptedException e2) {
log.error(e2);
}
}
log.error(e);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有