Watttttt
Watttttt

Reputation: 1

Handling URL Redirections with HttpURLConnection or HttpsURLConnection

I recently ran into an issue while trying use HttpURLConnection to get the response code of a website, specifically https://bobaguys.com

Here is the code

URL url = new URL("https://bobaguys.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
System.out.println(connection.getResponseCode());

And we see that we get an error Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching bobaguys.com found. After a tiny bit of investigation however, I realized that if I changed the URL to http://bobaguys.com or http://www.bobaguys.com, I get a redirection response of 301, with it seemingly being permanently moved to https://www.bobaguys.com. So I guess here is my first question. Why is http://bobaguys.com not redirected? Does the web server of the original site have to set up that redirection and they just never set it for https://bobaguys.com? And my second question is, how should I handle cases like this? I want to consider all these URLs as valid strings to feed to my function and return that the site is valid. There's the brute force option of just trying all the combinations of the two http and https protocols with www. and without www., but I feel like there must be a better way right?

Upvotes: 0

Views: 49

Answers (0)

Related Questions