Android HTTPURLConnection:HTTP响应始终403

发布于 2025-02-06 11:45:00 字数 2676 浏览 3 评论 0原文

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);
        // ...
    }
}

输入例如: https://wwww.youtube.com/watch?v=ht_ht_nvwreihg

这总是返回null。 HTTP响应代码为403。这就是为什么我尝试添加用户代理的原因,但这无济于事。

有什么方法可以解决此问题吗?我想在不使用YouTube API的情况下获取视频标题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2025-02-13 11:45:01

我尝试用卷发来获取它。您的URL使用http而不是https:// ...,将其更改为https://www.youtube.com/oembed?url= 。

尝试以http开头时尝试获取URL为我带来403状态。但是https是成功的。

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

应该更改为:

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

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");
昔日梦未散 2025-02-13 11:45:01

服务器不同意满足请求(错误代码403)。这可能是由于协议所致。尝试使用 httpsurlconnection 而不是httpurlconnection。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文