【问题标题】:Java: HTTPS URL not redirectingJava:HTTPS URL 未重定向
【发布时间】:2017-09-30 15:29:13
【问题描述】:

我正在尝试从 newURLConnection 获取重定向的 URL,但是当我打印出来时,它仍然会在重定向之前输出 URL 的内容。我确定原来的 newURLConnection 是一个合法的 URL,因为当我将它输入网络浏览器时,它会自动重定向。我已经尝试将 conn.setInstanceFollowRedirects 设置为 false 和 true,但两者都不起作用......由于某种原因,我无法获得重定向的 URL。我们将不胜感激。

//open a connection
                URLConnection newURLConnection = params[0].openConnection();
                HttpURLConnection conn =  (HttpURLConnection) newURLConnection;
                //following code sourced from mkyong.com
                conn.setInstanceFollowRedirects(true);
                //HttpURLConnection.setFollowRedirects(true);
                conn.connect();
                //get the redirected URL
                InputStream newURLConnectedStream = conn.getInputStream();
                System.out.println("" + conn.getURL());

【问题讨论】:

标签: java android http url redirect


【解决方案1】:

试试下面的代码来获取吧,

URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();

复制自java, android, resolve an url, get redirected uri

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 2012-09-27
    • 1970-01-01
    • 2023-03-18
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多