本文介绍了从Http中读取数据响应很少抛出BindException:地址已在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码从http请求中读取数据。
在一般情况下它运行良好,但有时候httpURLConnection.getResponseCode()会抛出java.net.BindException:地址已在使用中:connect
I use following code to read data form http request.In general cases it works good, but some time "httpURLConnection.getResponseCode()" throws java.net.BindException: Address already in use: connect
............
URL url = new URL( strUrl );
httpURLConnection = (HttpURLConnection)url.openConnection();
int responseCode = httpURLConnection.getResponseCode();
char charData[] = new char[HTTP_READ_BLOCK_SIZE];
isrData = new InputStreamReader( httpURLConnection.getInputStream(), strCharset );
int iSize = isrData.read( charData, 0, HTTP_READ_BLOCK_SIZE );
while( iSize > 0 ){
sbData.append( charData, 0, iSize );
iSize = isrData.read( charData, 0, HTTP_READ_BLOCK_SIZE );
}
.................
finally{
try{
if( null != isrData ){
isrData.close();
isrData = null;
}
if( null != httpURLConnection ){
httpURLConnection.disconnect();
httpURLConnection = null;
}
strData = sbData.toString();
}
catch( Exception e2 ){
}
在Java 1.6,Tomcat 6上运行的代码。
谢谢
The code running on Java 1.6, Tomcat 6.Thank you
推荐答案
摆脱disconnect()并关闭读者而不是。您正在耗尽本地端口,并使用disconnect()禁用HTTP连接池,这是解决方案。
Get rid of the disconnect() and close the Reader instead. You are running out of local ports, and using disconnect() disables HTTP connection pooling which is the solution to that.
这篇关于从Http中读取数据响应很少抛出BindException:地址已在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!