您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection超时设置

2008-10-30 18:33 489 查看
Java中可以使用HttpURLConnection来请求WEB资源。
HttpURLConnection对象不能直接构造,需要通过URL.openConnection()来获得HttpURLConnection对象,示例代码如下:
String szUrl = "http://www.ee2ee.com/";
URL url = new URL(szUrl);
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置超时(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。可以通过以下两个语句来设置相应的超时:
System.setProperty("sun.net.client.defaultConnectTimeout", 超时毫秒数字符串);
System.setProperty("sun.net.client.defaultReadTimeout", 超时毫秒数字符串);
其中: sun.net.client.defaultConnectTimeout:连接主机的超时时间(单位:毫秒)
sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒)
例如:
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
JDK 1.5以前的版本,只能通过设置这两个系统属性来控制网络超时。在1.5中,还可以使用HttpURLConnection的父类URLConnection的以下两个方法:
setConnectTimeout:设置连接主机超时(单位:毫秒)
setReadTimeout:设置从主机读取数据超时(单位:毫秒)
例如:
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(30000);
urlCon.setReadTimeout(30000);
需要注意的是,笔者在JDK1.4.2环境下,发现在设置了defaultReadTimeout的情况下,如果发生网络超时,HttpURLConnection会自动重新提交一次请求,出现一次请求调用,请求服务器两次的问题(Trouble)。我认为这是JDK1.4.2的一个bug。在JDK1.5.0中,此问题已得到解决,不存在自动重发现象。
import java.io.*;
import java.net.*;

/**
* @author chenzhimin
*
*/

public class TestUrl {
/**
* 只能用户HTTP协议
* @param photoUrl
* @param fileName
* @return
*/
public boolean saveUrlAs(String photoUrl, String fileName) {
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
DataInputStream in = new DataInputStream(connection
.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;

} catch (Exception e) {
System.out.println(e);
return false;
}
}
/**
* 兼容HTTP和FTP协议
* @param urlString
* @return
*/
public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();

try {

URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "/n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}

/**
*
* @param args
*/
public static void main(String[] args) {
TestUrl test = new TestUrl();
String photoUrl = "http://www.musichy.com/share/admin//20061120060438448.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "f:/bak/";
boolean flag = test.saveUrlAs(photoUrl, filePath + fileName);
System.out.println("Run ok!/n Get URL file " + flag);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: