import java.net.URI;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
...
ResponseEntity<?> result;
try {
String url = "domainname?sysparm_query=sys_class_name=asdf^u_numberSTARTSWITHAP122&fields=num,play_name&asdf_lt=5";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
URI uri = builder.build().toUri();
LOG.info(uri);
String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
// headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> entity = new HttpEntity<String>(headers);
result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result);
LOG.info(url);
if (result != null) {
LOG.info(result.getBody());
}
} catch (Exception e) {
result = new ResponseEntity<String>(LocalDateTime.now() + " :: Success", HttpStatus.OK);
e.printStackTrace();
}
上面的代码抛出以下异常
org.springframework.web.client.ResourceAccessException:GET请求上的I / O错误
但是,当我在浏览器或邮递员中尝试时 - 我收到了正确的结果。
答案 0 :(得分:0)
由于没有设置代理,这导致了。 设置代理后,它开始工作。