TestApi.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import java.io.*;
  2. import java.net.*;
  3. public class TestApi {
  4. public static void main(String[] args) {
  5. try {
  6. URL url = new URL("http://localhost:8448/system/businessScene");
  7. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  8. conn.setRequestMethod("POST");
  9. conn.setRequestProperty("Content-Type", "application/json");
  10. conn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE3MTg1NDkzODAxNzYsImV4cCI6MTcxODU1Mjk4MH0.SP8Jv6X8n4YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5YJ6K5Y");
  11. conn.setDoOutput(true);
  12. String jsonInputString = "{" +
  13. "\"sceneName\": \"TestScene\"," +
  14. "\"thirdPersonCameraPos\": \"[-2794881.16,5012908.69,2782554.46]\"," +
  15. "\"thirdPersonCameraTarget\": \"[-2794881.16,5012908.69,0]\"," +
  16. "\"thirdPersonCameraHeading\": 0.8474376369888388," +
  17. "\"thirdPersonCameraPitch\": -0.47481682300505734," +
  18. "\"thirdPersonCameraRoll\": 6.2831853071795845," +
  19. "\"thirdPersonCameraDirection\": \"[-0.25689388313349093,-0.9082555319630337,0.3302687110023337]\"," +
  20. "\"thirdPersonCameraUp\": \"[-0.6247398838094573,0.4168025198968015,0.660284587875124]\"," +
  21. "\"status\": \"0\"," +
  22. "\"remark\": \"Test\"" +
  23. "}";
  24. try (OutputStream os = conn.getOutputStream()) {
  25. byte[] input = jsonInputString.getBytes("utf-8");
  26. os.write(input, 0, input.length);
  27. }
  28. int code = conn.getResponseCode();
  29. System.out.println("Response code: " + code);
  30. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
  31. StringBuilder response = new StringBuilder();
  32. String responseLine = null;
  33. while ((responseLine = br.readLine()) != null) {
  34. response.append(responseLine.trim());
  35. }
  36. System.out.println("Response body: " + response.toString());
  37. conn.disconnect();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }