Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. String url = "http:localhost:8080/RestPractice/rest/UserService/path";
  2. URL obj = new URL(url);
  3. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  4.  
  5.  
  6. con.setRequestMethod("POST");
  7. con.setRequestProperty("User-Agent", USER_AGENT);
  8.  
  9. Gson gsonObj = new Gson();
  10. Map<String, String> inputMap = new HashMap<String, String>();
  11. inputMap.put("name", "somename");
  12.  
  13.  
  14. String jsonStr = gsonObj.toJson(inputMap);
  15. System.out.println(jsonStr);
  16.  
  17.  
  18. con.setDoOutput(true);
  19. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  20. wr.writeBytes(jsonStr);
  21. wr.flush();
  22. wr.close();
  23.  
  24. int responseCode = con.getResponseCode();
  25. System.out.println("Response Code : " + responseCode);
  26.  
  27. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  28. String output;
  29. StringBuffer response = new StringBuffer();
  30.  
  31. while ((output = in.readLine()) != null) {
  32. response.append(output);
  33. }
  34. in.close();
  35.  
  36.  
  37. System.out.println(response.toString());
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement