-1

So I'm trying to send a HTTPS request to my Flask application. Before, it worked successfully using just HTTP. Now, I wanted to change it to HTTPS.

But it does not work unfortunately. In my Java IDE, and my Python IDE, I get these errors:

javax.net.ssl.SSLException: Unsupported or unrecognized SSL message (Java client side)

code 400, message Bad request version ("\x1bè\x13\x16)... and "‚ ~ˆòÎ×ý›tYê >ôeïõ?2ŸÏËðÚü$ü^d ‰ô$NªO5~EN4ë=Y8õûb~ïÁ¢˜è) bÀ,À+Ì©À0̨À/ ŸÌª £ ž ¢À$À(À#À' k j g @À.À2À-À1À&ÀÀ%À)À" HTTPStatus.BAD_REQUEST* (Flask server side)

My code for sending the request is:

URL myURL = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) myURL.openConnection();
connection.setRequestMethod("GET");
System.out.println(connection.getResponseCode());
...

Basically, I just changed my connection object to Https, and also modified the URL string to use "https" instead. It worked perfect with HTTP, and now, nothing works with HTTPS.

What do I have to do to get it working with HTTPS?


EDIT: In my Flask application, I changed app.run to also include parameter ssl_context='adhoc'. Now I get a different exception:

javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Marie M.
  • 170
  • 1
  • 3
  • 13

1 Answers1

1

ssl_context='adhoc' is the right way.

This generates a custom SSL cert and you can access your Flask app via https.

But...

As this cert is self signed, your client (Browser, curl, or Java Application) does not trust the cert.

That is why you get error messages.

Have a look at the following blog post to learn more what you can do about it:

https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37