'How to create Atom+xml Post request in Java?

This post request is working fine in Postman , when I am try to implement in Java I am getting 400 badrequest , I have tried with spring resttemplate as well Iam getting same error 400 badrequest (Your client has issued a malformed or illegal request)

String url = "PostURL";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Bearer ", token);
con.setRequestProperty("Content-Type",
        "application/atom+xml;charset=utf-8");
String urlParameters = "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>    <apps:property name=\"publicKey\" value=\"DVNTVJ3ZGoxSjhhR3ZsWVlJQmNrOWYySnNJcEFLZExrb3ljZjRYM0INCk10Y0V2TEp2c1kveFZ2Y2FVNUZ5M2w5NmNLWC9mZGlmU3pacVY0UlN1SDFRQXc9PQ0KPVZBdUENCi0tLS0tRU5EIFBHUCBQVUJMSUMgS0VZIEJMT0NLLS0tLS0=/></atom:entry>";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();

BufferedReader in = new BufferedReader(new InputStreamReader(
        con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
System.out.println("response:" + response.toString());


} catch (IOException e) {
System.out.println("error" + e.getMessage());

Result: I am getting 400 badrequest

Is anything I am missing here??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source