问题
I have set responseTimeout and soTimeout to 15000ms but I still get a timeout after 90000ms.
I tested this on v1.3.18 and v1.3.17.
When I don't register jcifs my default timeout for HttpURLConnection occurs correctly after 15000ms :
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
But when I register jcifs then the timout occurs after 90000ms:
System.setProperty("jcifs.smb.client.responseTimeout", "15000");
System.setProperty("jcifs.smb.client.soTimeout", "15000");
jcifs.Config.registerSmbURLHandler();
[...]
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
It seems that the jcifs timeout and my default timeout are both ignored for another value.
I have also tried setProperty directly on Config but it does not change :
jcifs.Config.setProperty("jcifs.smb.client.responseTimeout", "15000");
jcifs.Config.setProperty("jcifs.smb.client.soTimeout", "15000");
回答1:
(This message was posted to jcifs forum at http://thread.gmane.org/gmane.network.samba.java/9554)
For me the problem is that jcifs wraps a new HttpURLConnection so it loses every settings defined on the original connection, like the timeout settings. To prove this either I use reflection or I modify the library and change jcifs internal connection, then the timeout works fine.
(For information setting jcifs.smb.client.responseTimeout and jcifs.smb.client.soTimeout does not work)
First, I validate that jcifs is the problem : my timeout of 15000ms does not work at all when I use jcifs.Config.registerSmbURLHandler(), connection breaks after 30000ms. My 15000ms timeout works only if I remove the call to registerSmbURLHandler().
Regarding the problem, I open a connection (with jcifs previously registered) :
URLConnection myConnection = new URL(url).openConnection();
Then the URLStreamHandler creates a wrapping NtlmHttpURLConnection and hides the real HttpURLConnection :
protected URLConnection openConnection(URL url) throws IOException {
url = new URL(url, url.toExternalForm(),
getDefaultStreamHandler(url.getProtocol()));
return new NtlmHttpURLConnection((HttpURLConnection)
url.openConnection());
}
So my timeout settings are applied to the wrapper NtlmHttpURLConnection, it's not applied to the true opened URLConnection. So my timeout are useless :
myConnection.setReadTimeout(15000); // applied to the new NtlmHttpURLConnection(wrapped), not the real wrapped one
myConnection.setConnectTimeout(15000); // applied to the new NtlmHttpURLConnection(wrapped), not the real wrapped one
There are two solutions I can use to change the timeout on the wrapped connection : with reflection or with a fixed library.
With reflection, I access the private wrapped connection and change the private fields connectTimeout and readTimeout :
Class<?> classConnection = myConnection.getClass();
Field privateFieldURLConnection = classConnection.getDeclaredField("connection");
privateFieldURLConnection.setAccessible(true);
URLConnection privateURLConnection = (URLConnection) privateFieldURLConnection.get(myConnection);
Class<?> classURLConnectionPrivate = privateURLConnection.getClass();
Field privateFieldConnectTimeout = classURLConnectionPrivate.getDeclaredField("connectTimeout");
privateFieldConnectTimeout.setAccessible(true);
privateFieldConnectTimeout.setInt(privateURLConnection, 15000);
Field privateFieldReadTimeout = classURLConnectionPrivate.getDeclaredField("readTimeout");
privateFieldReadTimeout.setAccessible(true);
privateFieldReadTimeout.setInt(privateURLConnection, 15000);
Or I modify the jcifs library and the constructor NtlmHttpURLConnection() :
public NtlmHttpURLConnection(HttpURLConnection connection) {
super(connection.getURL());
this.connection = connection;
this.connection.setReadTimeout(15000);
this.connection.setConnectTimeout(15000);
requestProperties = new HashMap();
}
来源:https://stackoverflow.com/questions/38415882/timeout-defined-with-jcifs-does-not-work