HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to mydomain.com not permitted by network security policy

h‮ww//:sptt‬w.lautturi.com
HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to mydomain.com not permitted by network security policy

The java.net.UnknownServiceException: CLEARTEXT communication to mydomain.com not permitted by network security policy error is thrown when you try to make an HTTP connection to a server using an insecure (cleartext) protocol, such as HTTP, and the network security policy of the device or system does not allow cleartext communication.

This error can occur when you are using the HttpURLConnection or URLConnection classes in Java to make an HTTP request, and the server you are trying to connect to is configured to use an insecure protocol.

To fix this error, you need to ensure that you are using a secure protocol, such as HTTPS, when making the HTTP connection. You can do this by using the https:// prefix in the URL of the server, or by using a secure wrapper, such as the HttpsURLConnection class, to make the connection.

Here is an example of how to use the HttpsURLConnection class in Java to make an HTTP request using a secure protocol:

URL url = new URL("https://mydomain.com/api");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();

In this example, the HttpsURLConnection class is used to make a secure connection to the mydomain.com server using the HTTPS protocol. The setRequestMethod method is used to specify the HTTP method, such as GET or POST, and the setDoInput and setDoOutput methods are used to enable input and output streams for the connection.

By using a secure protocol, you can avoid the java.net.UnknownServiceException: CLEARTEXT communication to mydomain.com not permitted by network security policy error and ensure that your HTTP connections are secure and compliant with the network security policy.

Created Time:2017-11-01 22:29:43  Author:lautturi