Home > OS >  Android HttpURLConnection: HTTP response always 403
Android HttpURLConnection: HTTP response always 403

Time:06-11

public static class GetTitleTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {
        try {
            String youtubeUrl = urls[0];
            if (youtubeUrl != null) {
                URL url = new URL("http://www.youtube.com/oembed?url="   youtubeUrl   "&format=json");

                HttpURLConnection connection = null;
                BufferedReader reader = null;

                try {
                    connection = (HttpURLConnection) url.openConnection();
                    connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:221.0) Gecko/20100101 Firefox/31.0");
                    connection.connect();

                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        Log.d(TAG, "HTTP response code is "   connection.getResponseCode());
                        return null;
                    }

                    InputStream stream = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(stream));
                    StringBuffer buffer = new StringBuffer();

                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line "\n");
                        Log.d("Response: ", "> "   line);
                    }

                    return buffer.toString();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String title) {
        Log.v(TAG, "onPostExecute: "   title);
        super.onPostExecute(title);
        // ...
    }
}

Input for example: https://www.youtube.com/watch?v=hT_nvWreIhg

This always returns null. The HTTP response code is 403. That is why I tried adding a user agent, but it didn't help.

Is there any way to fix this? I would like to get the video title without using the YouTube API.

CodePudding user response:

I tried fetching it with curl. Your URL uses http instead of https://..., change it to https://www.youtube.com/oembed?url=.

Trying to fetch the URL when it starts with just http resulted in a 403 status for me. But https was successful.

URL url = new URL("http://www.youtube.com/oembed?url="   youtubeUrl   "&format=json");

Should be changed to:

URL url = new URL("https://www.youtube.com/oembed?url="   youtubeUrl   "&format=json");

CodePudding user response:

The server does not agree to fulfil the request (error code 403). This is probably due to the protocol. Try using HttpsURLConnection instead of HttpURLConnection.

  • Related