Guten Tag,
ich versuche gerade auf einen Spring Webservice welcher ein JSON POST body erwartet einen request zu senden. Leider gelingt es mir nicht. Ich erhalte jedes mal die gleiche Fehlermeldung:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at Main.main(Main.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.uiDesigner.snapShooter.SnapShooter.main(SnapShooter.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Immer wenn ich etwas neues mit der Connection machen möchte wird die Exception geworfen. Wer kann mir weiterhelfen??? Wenn ich die Verbindung ohne https versuche klappt alles.
ObjectMapper mapper = new ObjectMapper();
Person person = new Person();
person.setName("TEST");
person.setVorname("Hans3");
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
final X509Certificate cert1 = (X509Certificate) certificateFactory.generateCertificate(Main.class.getResourceAsStream("domain-CA.crt"));
TrustManager[] trustManager = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
if (!x509Certificates[0].equals(cert1)) {
throw new CertificateException();
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManager, new SecureRandom());
String jsonPerson = mapper.writeValueAsString(person);
URL url = new URL("https://phonebook.domain.de/add");
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type",
"application/json");
connection.setRequestProperty("Content-Length", String.valueOf(jsonPerson.length()));
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(jsonPerson);
writer.close();
connection.connect();
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
for (String line; (line = reader.readLine()) != null; ) {
System.out.println(line);
}
reader.close();
}
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}```