* @return <code>HttpURLConnection</code> ready for .connect
* @exception IOException
* if an I/O Exception occurs
*/
protected HttpURLConnection setupConnection(URL u, String method, HTTPSampleResult res) throws IOException {
SSLManager sslmgr = null;
if (HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(u.getProtocol())) {
try {
sslmgr=SSLManager.getInstance(); // N.B. this needs to be done before opening the connection
} catch (Exception e) {
log.warn("Problem creating the SSLManager: ", e);
}
}
final HttpURLConnection conn;
final String proxyHost = getProxyHost();
final int proxyPort = getProxyPortInt();
if (proxyHost.length() > 0 && proxyPort > 0){
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
//TODO - how to define proxy authentication for a single connection?
// It's not clear if this is possible
// String user = getProxyUser();
// if (user.length() > 0){
// Authenticator auth = new ProxyAuthenticator(user, getProxyPass());
// }
conn = (HttpURLConnection) u.openConnection(proxy);
} else {
conn = (HttpURLConnection) u.openConnection();
}
// Update follow redirects setting just for this connection
conn.setInstanceFollowRedirects(getAutoRedirects());
int cto = getConnectTimeout();
if (cto > 0){
conn.setConnectTimeout(cto);
}
int rto = getResponseTimeout();
if (rto > 0){
conn.setReadTimeout(rto);
}
if (HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(u.getProtocol())) {
try {
if (null != sslmgr){
sslmgr.setContext(conn); // N.B. must be done after opening connection
}
} catch (Exception e) {
log.warn("Problem setting the SSLManager for the connection: ", e);
}
}