yahoofinance.util.RedirectableRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of YahooFinanceAPI Show documentation
Show all versions of YahooFinanceAPI Show documentation
This library provides some methods that should make it easy
to communicate with the Yahoo Finance API.
It allows you to request detailed information, some statistics
and historical quotes on stocks.
Separate functionality is available to request a simple FX quote.
Please check the javadoc to get a complete overview of the available methods
and to get an idea of which data is available from Yahoo Finance.
package yahoofinance.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Stijn Strickx
*/
public class RedirectableRequest {
private URL request;
private int protocolRedirectLimit;
private int connectTimeout = 10000;
private int readTimeout = 10000;
public RedirectableRequest(URL request) {
this(request, 2);
}
public RedirectableRequest(URL request, int protocolRedirectLimit) {
this.request = request;
this.protocolRedirectLimit = protocolRedirectLimit;
}
public URLConnection openConnection() throws IOException {
return openConnection(new HashMap());
}
public URLConnection openConnection(Map requestProperties) throws IOException {
int redirectCount = 0;
boolean hasResponse = false;
HttpURLConnection connection = null;
URL currentRequest = this.request;
while(!hasResponse && (redirectCount <= this.protocolRedirectLimit)) {
connection = (HttpURLConnection) currentRequest.openConnection();
connection.setConnectTimeout(this.connectTimeout);
connection.setReadTimeout(this.readTimeout);
for(String requestProperty : requestProperties.keySet()) {
connection.addRequestProperty(requestProperty, requestProperties.get(requestProperty));
}
// only handle protocol redirects manually...
connection.setInstanceFollowRedirects(true);
switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
redirectCount++;
String location = connection.getHeaderField("Location");
currentRequest = new URL(request, location);
break;
default:
hasResponse = true;
}
}
if(redirectCount > this.protocolRedirectLimit) {
throw new IOException("Protocol redirect count exceeded for url: " + this.request.toExternalForm());
} else if(connection == null) {
throw new IOException("Unexpected error while opening connection");
} else {
return connection;
}
}
public URL getRequest() {
return request;
}
public void setRequest(URL request) {
this.request = request;
}
public int getProtocolRedirectLimit() {
return protocolRedirectLimit;
}
public void setProtocolRedirectLimit(int protocolRedirectLimit) {
if(protocolRedirectLimit >= 0) {
this.protocolRedirectLimit = protocolRedirectLimit;
}
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy