Sach_IN
Sach_IN

Reputation: 31

Java Https POST fails with connection timed out but works with Postman. Can anyone help me out?

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();

Can anyone tell me whats wrong with my code?

Upvotes: 2

Views: 2128

Answers (3)

NEERAJ
NEERAJ

Reputation: 1

I also faced similar problem but mine was more of a typo. I mixed up between http and https.

Upvotes: 0

Sach_IN
Sach_IN

Reputation: 31

I was using the office internet which might be using some proxies.So I tried using my mobile Internet through Hotspot and I was able to fetch the results.

This might happen due to some proxy preventing it from fetching the results.

Thanks

Upvotes: 0

Sven
Sven

Reputation: 21

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/

Upvotes: 1

Related Questions