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/
我正在使用办公室互联网,可能正在使用一些代理。所以我尝试通过热点使用我的移动互联网,并且能够获取结果。
这可能是由于某些代理阻止它获取结果而发生的。
谢谢
我也遇到了类似的问题,但我的更多是一个打字错误。我混淆了 http 和 https。