· 9 years ago · Dec 08, 2016, 05:17 PM
1public static void post() throws Exception{
2
3 CloseableHttpClient httpclient = HttpClients.createDefault();
4
5 try{
6 // POST
7 HttpPost httpPost = new HttpPost("https://testservices.informatsoftware.be/icursisten/1/course/42CD5A20-E22F-45D7-973C-FAB8734DEC50");
8
9 httpPost.setEntity(new StringEntity("",
10 ContentType.create("application/json")));
11 CloseableHttpResponse response2 = httpclient.execute(httpPost);
12
13 //httpPost.
14
15 httpPost.addHeader("InstellingsNr", "128521");
16 httpPost.addHeader("Authentication","pubKey_HbKycQkxTipifyplssirJPQZxEHFbL" + ":" + encode("zmHnwWgjWj37H5yjm1Or1MtfLyRPoYcwJytE4VV5Y6E1Ep0BpSpQ7048tNLp3RxcJlq5H4", ""));
17 httpPost.addHeader("Timestamp", LocalDateTime.now().toString());
18 httpPost.addHeader("Accept","application/json");
19 httpPost.addHeader("Content-Typet","application/json");
20 httpPost.addHeader("Content-Length","0");
21
22 try {
23 System.out.println("Request headers: \n");
24 for(Header hdr : httpPost.getAllHeaders()){
25 System.out.println("\t" + hdr.toString());
26 }
27
28 System.out.println("\n\nResult: \n");
29
30 System.out.println("\t" + response2.getStatusLine());
31 HttpEntity entity2 = response2.getEntity();
32 // do something useful with the response body
33 // and ensure it is fully consumed
34 BufferedReader in = new BufferedReader(
35 new InputStreamReader(entity2.getContent()));
36 String inputLine;
37 StringBuffer response = new StringBuffer();
38
39 while ((inputLine = in.readLine()) != null) {
40 response.append(inputLine);
41 }
42 in.close();
43
44 //print result
45 System.out.println("\t" + "Response : " + response.toString());
46 EntityUtils.consume(entity2);
47 }
48 finally {
49 response2.close();
50 System.out.println("**********************************");
51 }
52 }finally{
53 httpclient.close();
54 }
55 }
56
57 public static String encode(String key, String data) throws Exception {
58 Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
59 SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
60 sha256_HMAC.init(secret_key);
61
62 return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
63 }
64
65 public static void main(String[] args) throws Exception {
66 post();
67
68 }