我使用以下代码从 http 请求中读取数据。 在一般情况下,它运行良好,但有时“httpURLConnection.getResponseCode()”会抛出 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。 谢谢
最佳答案
去掉 disconnect() 并关闭 Reader。您的本地端口用完了,使用 disconnect() 会禁用 HTTP 连接池,这是解决该问题的方法。
关于java - 从 Http 响应读取数据很少引发 BindException : Address already in use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4205210/