0

I am calling a Rest API from my java application but am receiving 302 error but when I tried to call it from the advanced REST client I received 200 which is successful response .


        try {

            URL url = new URL("http://jsonmock.hackerrank.com/api/football_matches?year="+year+"&page=2");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
        return 100;
    }
yasmin
  • 15
  • 6
  • 302 means it's been moved. It's redirecting you to the https version. – Federico klez Culloca Sep 24 '20 at 09:10
  • And what is wrong with 302? Have you read an overview about http status code? – Reporter Sep 24 '20 at 09:10
  • @FedericoklezCulloca ,, but why it worked successfully on the advanced REST client ? the request may have https but when I tried to call it with s I received : javax.net.ssl.SSLException: Received fatal alert: protocol_version – yasmin Sep 24 '20 at 09:13
  • @FedericoklezCulloca yes , but it not reading the data from the API in my console – yasmin Sep 24 '20 at 09:14
  • I don't know Advanced REST client, but at this point I wonder whether it follows redirects... For the issue with https, see if https://stackoverflow.com/questions/16541627/javax-net-ssl-sslexception-received-fatal-alert-protocol-version helps – Federico klez Culloca Sep 24 '20 at 09:17
  • @yasmin try this one https://mkyong.com/java/java-httpurlconnection-follow-redirect-example/ – Niyas Sep 24 '20 at 10:52

0 Answers0