0

Here is my code:

public String readTheUrl(String place) throws IOException {
        String data = "";
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection = null;

        try {
            URL url = new URL(place);

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);

            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setAllowUserInteraction(false);

            httpURLConnection.connect();

            int response=httpURLConnection.getResponseCode();

            inputStream = httpURLConnection.getInputStream();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();

            String line = "";

            while ((line = (bufferedReader.readLine())) != null) {
                stringBuffer.append(line);
            }

            data = stringBuffer.toString();
            bufferedReader.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        inputStream.close();
        httpURLConnection.disconnect();

        return data;
    }

When I am using this code for loading other url, it is working perfectly, but in case of

"https://api-crt.cert.havail.sabre.com/v1/shop/flights?origin=FRA&destination=DFW&departuredate=2019-10-28&returndate=2019-11-10&pointofsalecountry=DE"

it always return null. I tested this api with postman, and I loaded JSON file. Is there any problem with url or it needs some specific loading? Thank you!

  • 2
    A GET request to the given URL returns a 401 error. You should see that in the stack trace you're printing. Don't you? What do you expect to happen? No 401 error, or a 401 error, but a response body anyway? If the former, you need to be authenticated. If the latter, you need to use https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#getErrorStream-- – JB Nizet Sep 22 '19 at 12:06
  • How can I authenticate myself? When I loaded JSON in postman, I used Bearer token, can this be used here as well? I am beginner with this area, so sorry if I am asking something that you said in first message. Thank you! – Andrija Randjelovic Sep 22 '19 at 13:25
  • Yes, it can be used too. HttpUrlConnection allows setting headers using the awkwardly named `setRequestProperty` method. – JB Nizet Sep 22 '19 at 14:05
  • httpURLConnection.setRequestProperty("Content-Type", "application/" + "json"); httpURLConnection.setRequestProperty("Authorization", "Bearer " + tokenca); I added these two lines, String tokenca is my bearer token. It still sends the same error... – Andrija Randjelovic Sep 22 '19 at 14:07

0 Answers0