Skip to content
This repository was archived by the owner on Nov 21, 2017. It is now read-only.

Commit 8caf18b

Browse files
committed
Add timeout parameter to connections
1 parent a1d19bb commit 8caf18b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/main/java/com/limewoodMedia/nsapi/NationStates.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@
6767
import org.apache.http.client.params.ClientPNames;
6868
import org.apache.http.conn.params.ConnRoutePNames;
6969
import org.apache.http.impl.client.DefaultHttpClient;
70+
import org.apache.http.params.BasicHttpParams;
7071
import org.apache.http.params.CoreProtocolPNames;
72+
import org.apache.http.params.HttpConnectionParams;
73+
import org.apache.http.params.HttpParams;
7174
import org.kxml2.io.KXmlParser;
7275
import org.xmlpull.v1.XmlPullParser;
7376
import org.xmlpull.v1.XmlPullParserException;
@@ -96,6 +99,7 @@ public NationStates() {
9699
private long hardRateLimit = 0;
97100
private String proxyIP;
98101
private int proxyPort;
102+
private long timeout = 15000;
99103

100104
/**
101105
* Sets the rate limit - default is 49 (per 30 seconds)
@@ -191,6 +195,14 @@ public synchronized int getProxyPort() {
191195
public synchronized void setProxyPort(int port) {
192196
this.proxyPort = port;
193197
}
198+
199+
public synchronized long getTimeout() {
200+
return timeout;
201+
}
202+
203+
public synchronized void setTimeout(long timeout) {
204+
this.timeout = timeout;
205+
}
194206

195207
/**
196208
* In relaxed mode the XML parsing attempts to correct any invalid xml characters it encounters
@@ -1362,11 +1374,14 @@ private synchronized InputStream doRequest(String url) throws IOException {
13621374
System.out.println("Making HTTP request: " + url);
13631375
}
13641376

1365-
HttpClient client = new DefaultHttpClient();
1377+
final HttpParams httpParams = new BasicHttpParams();
1378+
HttpClient client = new DefaultHttpClient(httpParams);
13661379
if (proxyIP != null) {
13671380
HttpHost proxy = new HttpHost(proxyIP, proxyPort);
13681381
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
13691382
}
1383+
HttpConnectionParams.setConnectionTimeout(client.getParams(), (int) timeout);
1384+
HttpConnectionParams.setSoTimeout(client.getParams(), (int) timeout);
13701385
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, this.userAgent);
13711386
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
13721387
HttpGet get = new HttpGet(url);

0 commit comments

Comments
 (0)