setConnectTimeout无效果

在项目我碰到一个问题就是需要判断公网和专网,那么我是这么写一个方法的,方法代码如下:

// 判断是否能够访问外网
	public static boolean openUrl() {
		HttpURLConnection conn = null;
		String myString = "";
		InputStream is = null;
		BufferedInputStream bis = null;
		try {
			URL url = new URL("http://www.baidu.com/index.html");
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(1500);
			conn.setRequestMethod("GET"); // 设定请求方式
			conn.connect(); // 建立到远程对象的实际连接
			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				System.out.println("网络错误异常!!!!");
				return false;
			}
			is = conn.getInputStream();
			bis = new BufferedInputStream(is);
			// 用ByteArrayBuffer缓存
			ByteArrayBuffer baf = new ByteArrayBuffer(50);
			int current = 0;
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
			myString = EncodingUtils.getString(baf.toByteArray(), "UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				bis.close();
				is.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (conn != null) {
				conn.disconnect(); // 中断连接
			}
		}
		if (myString.indexOf("baidu.com") > -1) {
			return true;
		} else {
			return false;
		}
	}

在代码中我们可以看到我设置了连接超时的时间为1500毫秒

conn.setConnectTimeout(1500);

,但是实际的情况是当时专网的时候(不能访问百度)根本不是1.5秒抛出超时的异常,这个问题困扰2天,

也在网上查询很多资料,结果有人说还要设置读取超时conn.setReadTimeout(1500);结果一试还是不行,

就再找发现有人在使用的情况下查看控制台发现访问1500毫秒之后系统会自动的尝试很多次(集体多少次没有算)

导致很长时间才抛出访问超时异常,这样导致我们看到的现象就是我设置了1500毫秒超时结果几分钟才给我超时异常,感觉设置的超时时间没有效果。

那么说一下我的解决方法,我个人测试通过,如果有好的方法请指出:

因为系统会自动的尝试很多次超时之后才给你抛出异常,所以我把这个异常超时时间设置短一点如我设置500毫秒,公网的情况下能正常的访问,专网也能很快的抛出异常达到了我的目的。

另外判断公网和专网的另一种方法就是用ping ,记得要起一个服务区处理ping,关闭之后要杀死改进程,否则第二次ping可能就阻塞了。

时间: 2024-08-28 17:10:15

setConnectTimeout无效果的相关文章

opacity在IE6~8下无效果,解决的办法

opacity在IE6~8下无效果,解决的办法 问题出现时rgba()在ie6下不出效果,最后查到是opacity的问题. opacity是css3时出现的,目前主流浏览器都支持.but老IE是个麻烦的问题. 查找网络后,发现解决办法大多都没用了. filter:alpha(opacity=50);     X 总结网上经验: 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset='utf-8'> 5 <

Unity3D烘培无效果原因

调节以下设置 Unity3D烘培无效果原因

解决android手机EditText设置光标颜色,android:textCursorDrawable=&quot;@drawable/corner_cursor&quot; 华为手机无效果的问题

<EditText android:id="@+id/alertdialog_zhuan_zeng_friend_phone_Edt" android:layout_width="match_parent" android:layout_height="40dp" android:textCursorDrawable="@drawable/corner_cursor" android:singleLine="t

xml文件的根节点layout_width或者layout_height设置无效果的原因分析

在android开发中相信大家对ListView.GridView等组建都很熟悉,在使用它们的时候需要自己配置相关的Adapter,并且配置现骨干的xml文件作为ListView等组建的子View,这些xml文件在Adapter的getView方法中调用.例如: public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null) { convertView = App.getL

iOS UIButton加在window上点击无效果问题

UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; self.windo

jquery easyui使用(二)&#183;&#183;&#183;&#183;&#183;&#183;可折叠面板动态加载无效果

先上代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>用车管理</title> <link href="../

给子元素设置margin-top无效果的一种解决方法

在写一个登陆界面的时候,设置登录按钮的margin-top时出了问题 先是这么写的 <div style="margin-top:30px"> <a style="float:left">注册</a><a style="float:right">找回密码</a> </div><input type="submit" name=""

解决 js setTimeout 传递带参数的函数无效果

最近 js  用到 setTimeout 递归调用 刷新进度  setTimeout ("getProgress(name,type)", 3000) ; 发现getProgress函数 带上参数 就失效了,不带参数可以调用又达不到效果 解决办法:匿名函数包装  改成如下即可  setTimeout(function(){getProgress(name, type)}, 3000);   原文地址:https://www.cnblogs.com/ysx215/p/9057463.ht

css3文字渐变无效果的解决方案

现在css3越来月流行了,为了实现一些高大上的效果,我们会用一些渐变的特效,请看文字渐变的特效代码: .title { font-size: 60px; line-height: 80px; text-align: center; margin-top: 15%; -webkit-background-clip: text; background: linear-gradient(to right, red, blue); color: transparent; } <div class="