Android:HttpURLConnection 重定向 - 可能已缓存

Android: HttpURLConnection redirects - probably cached

当我有多个重定向和多个调用时,HTTP 调用出现问题。让我们有以下代码:

con = (HttpURLConnection) (new
URL("http://server/?function=auth/fetch_internal_ip")).openConnection();
HttpURLConnection.setFollowRedirects(false);
con.setRequestMethod("GET");
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.connect(); StringBuffer buffer = new StringBuffer();
is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
buffer.append(line).append("\r\n");
is.close();
con.disconnect();
is = null;
ip = buffer.toString().trim();

第一次使用时效果很好,但以后的任何其他使用都不会遵循重定向。只有重新安装应用程序或重新启动 phone 有帮助。

哪里有问题?

你的问题不是很清楚,但如果你的请求被缓存有问题,你可以尝试使用 con.setUseCaches(false)

HttpURLConnection.setFollowRedirects(false); : 这会为以下连接禁用全局重定向,如果你想要重定向,你必须删除这一行。