I am getting mad with this code below.
When i send the request using the string directInput, it's working fine. Unfortunately when i used the json object converted into string jsonInput i got 404 error code. the directInput and jsonInput return the same values.
Someone could please help troubleshooting ? Thanks
public class Test {
public static void main(String[] args) {
try {
String userCredentials = "USR28:YG739G5XFVPYYV4ADJVW";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
URL url = new URL("http://74.208.84.251:8221/QosicBridge/user/deposit");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setRequestProperty ("Authorization", basicAuth);
//String directInput = "{\"msisdn\":\"22997858711\",\"amount\":1400,\"transref\":\"QOVNPVTRATYCBK8VIL1A\",\"clientid\":\"UBHQ\"}";
DepositObject d = new DepositObject();
d.setMsisdn("22997858711");
d.setAmount(35);
d.setTransref("QOVNPVTRATYCBK8VIL1A");
d.setClientid("UHBQ");
Gson gson = new Gson();
String jsonInput = gson.toJson(d).toString();
OutputStream os = conn.getOutputStream();
os.write(jsonInput.getBytes());
os.flush();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}