1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| 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.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty ("Authorization", basicAuth);
//String directInput = "{\"msisdn\":\"22997858711\",\"amount\":1400,\"transref\":\"JDQY78J6ABX68F0T56FP\",\"clientid\":\"UBHQ\"}";
DepositObject d = new DepositObject();
d.setMsisdn("22997858711");
d.setAmount(1400);
d.setTransref("JDQY78J6ABX68F0T56FP");
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();
}
} |
Partager