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 51 52 53 54 55 56 57 58 59 60 61 62 63
| 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\":\"QOVNPVTRAF69BK1VIL1A\",\"clientid\":\"UBHQ\"}";
DepositObject d = new DepositObject();
d.setMsisdn("22997858711");
d.setAmount(35);
d.setTransref(generateTransactionReference(20));
d.setClientid("UHBQ");
Gson gson = new Gson();
String jsonInput = gson.toJson(d).toString();
System.out.println(jsonInput);
OutputStream os = conn.getOutputStream();
os.write(directInput.getBytes(StandardCharsets.UTF_8));
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();
}
}
private static String generateTransactionReference(int len) {
char[] ch = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] c=new char[len];
Random random=new Random();
for (int i = 0; i < len; i++) {
c[i]=ch[random.nextInt(ch.length)];
}
return new String(c);
}
} |
Partager