7

I have a web server running on different machine. I am able to use postman client and load cert.pem and key.pem files and successfully do a get request.

Now I want to do this programmatically in Java. To do that I added the PKCS12 to the keystore as below:

keytool -importkeystore -destkeystore keystore.jks -srcstoretype PKCS12 -srckeystore keystore.p12

keytool -list -keystore keystore.jks

Now in the code I did this:

String url = "http://localhost:9443/server/api/v1/"+id+"/_history/"+history;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

System.setProperty("javax.net.ssl.keyStore", "/Users/rc/Downloads/test101/jks/keystore.jks");

System.setProperty("javax.net.ssl.keyStorePassword", "asdfgh");
System.setProperty("javax.net.ssl.keyStoreType", "JKS");            

con.setRequestMethod("GET");

        //add request header
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");

int responseCode = con.getResponseCode();       
System.out.println("\nSending 'GET' request to URL : " + url);      
System.out.println("Response Code : " + responseCode);

I am getting this error:

java.net.SocketException: Unexpected end of file from server at this line :
int responseCode = con.getResponseCode();

4
  • 1
    You need to set the system properties before connecting. Commented Apr 10, 2018 at 22:33
  • I moved the system properties to line before connection. I still see same error Commented Apr 10, 2018 at 22:41
  • This was so helpful to me. But why did it work with JDK 18 without installing the cert but it throws this error with JDK 8? Commented Jan 12, 2023 at 19:20
  • @tarekahf JDK 8, you mean the version from 2014, nearly a decade ago? Most likely this technology/process was just not supported by Java way back then. Commented Sep 5 at 15:18

2 Answers 2

5

You are trying to connect via http://, but should use https://. Look at your URL:

String url = "http://localhost:9443/server/api/v1/"+id+"/_history/"+history;

"java.net.SocketException: Unexpected end of file" means that the server accepted and then closed the connection without sending a response. I believe port 9443 for HTTPS connection is not ready to accept HTTP connection.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing it out. I fixed it and see the below error: Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching localhost found.
@javaMan When you resolve an error you've asked about thanks to an answer you've received, and you have a new error, please ask a new question (via clicking the "Ask question" button at the top of the site); don't edit your existing question to ask continuously more things.
-1

Follow the below step to add SSL Certificate in you application API Call.

Step : 1 Download SSL Certificate from web by following below steps

  • Go to your website
  • Click on lock button in URL bar
  • A Popup will appear -> Click on -> Connection is secure
  • Click on certificate button
  • Click on Details Tab -> Export it save it.
  • e.g certificate.crt

Step : 2

  • Go to android studio project
  • Add the certificate file in res/raw
  • Create xml resource directory within res/xml
  • Create network_security_config.xml file inside res/xml dir.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">yourdomain.com</domain>
        <trust-anchors>
            <certificates src="@raw/certificate"/>
        </trust-anchors>
    </domain-config>
</network-security-config>

Step : 3

  • Go to AndroidManifest.xml
  • Inside application tag add the below line

android:networkSecurityConfig="@xml/network_security_config"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.