Skip to content

Instantly share code, notes, and snippets.

@floriankammermann
Last active December 2, 2023 03:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floriankammermann/b5601de3e3f6977a5c1b45d3d91cb0b7 to your computer and use it in GitHub Desktop.
Save floriankammermann/b5601de3e3f6977a5c1b45d3d91cb0b7 to your computer and use it in GitHub Desktop.
URL Connection Test
import javax.net.ssl.*;
import java.io.IOException;
import java.net.*;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class URLTest {
public static void main(String[] args) {
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch(Exception e) {
System.err.println("exception while initializing SSLContext");
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
URL url = new URL(args[0]);
Proxy proxy = getProxy(args[0]);
System.out.println(proxy.toString());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
StringBuilder builder = new StringBuilder();
builder.append(httpURLConnection.getResponseCode())
.append(" ")
.append(httpURLConnection.getResponseMessage())
.append("\n");
Map<String, List<String>> map = httpURLConnection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet())
{
if (entry.getKey() == null)
continue;
builder.append( entry.getKey())
.append(": ");
List<String> headerValues = entry.getValue();
Iterator<String> it = headerValues.iterator();
if (it.hasNext()) {
builder.append(it.next());
while (it.hasNext()) {
builder.append(", ")
.append(it.next());
}
}
builder.append("\n");
}
System.out.println(builder);
} catch (IOException e) {
System.err.println("Error creating HTTP connection");
e.printStackTrace();
}
}
private static Proxy getProxy(String url) {
List<Proxy> l = null;
try {
ProxySelector def = ProxySelector.getDefault();
l = def.select(new URI(url));
ProxySelector.setDefault(null);
} catch (Exception e) {
e.printStackTrace();
}
if (l != null) {
for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
java.net.Proxy proxy = iter.next();
return proxy;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment