URL url = new URL(“My url”);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
Base64EncoderDecoder encoder = new Base64EncoderDecoder();
String encoded = encoder.encodeToString(userName+”:”+password);
conn.setRequestProperty(AUTHORIZATION, BASIC + encoded);
conn.setRequestMethod(POST);
conn.setRequestProperty(CONTENT_TYPE, FORM_URL_ENCODED);
conn.setConnectTimeout(10000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
谁能告诉我我的代码有什么问题?
最佳答案
private static void getEmployees()
{
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result);
}
https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/
关于Java Https POST 因连接超时而失败,但适用于 Postman。谁能帮我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59043891/