Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Android HttpURLConnection: HTTP response always 403

Posted on November 11, 2022 By

Solution 1 :

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

Solution 2 :

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.

Problem :

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.

READ  [FIXED] android - how to change status bar color on phones with front camera or ear speaker under the status bar area (while using full screen theme)?
Powered by Inline Related Posts
Android Tags:android, http, java, youtube

Post navigation

Previous Post: [FIXED] android developer api – there is a ‘Late Initialization Error’ during flutter development
Next Post: [FIXED] android – How to set SSL pinning using retrofit for Firebase Realtime Database Url?

Related Posts

[FIXED] java – Android studio Geocoder get location category Android
[FIXED] android studio – How to add numbers together using and If statement? Android
[FIXED] How I can show my android constrain layout chain icon hints, I did not show this feature in my layout. I am using android version 3.5.2 Android
[FIXED] androidx – How should i convert my AppTheme style to Material component Android
[FIXED] android – Align ImageView to top right corner Android
[FIXED] android – Getting statistics data from Google Store through API Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme