url().openStream 的超时问题处理

时间:2017-05-23 11:53:46   收藏:0   阅读:708
1 InputStream is = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip").openStream(); 
2 BufferedReader rd = new BufferedReader(InputStreamReader(is, Charset.forName("UTF-8"))); 

 上图可能出现 超时的情况。

 1 URL url = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip);
 2 HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
 3 htpcon.setRequestMethod("GET");
 4 htpcon.setDoOutput(true);
 5 htpcon.setDoInput(true);
 6 htpcon.setUseCaches(false);
 7 htpcon.setConnectTimeout(1000);
 8 htpcon.setReadTimeout(1000);
 9 InputStream in = htpcon.getInputStream();
10 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

 

其实url的openStream就是把openConnection和getInputStream连起来调用了。

原文:http://www.cnblogs.com/jinhaidong/p/6893242.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!