我正在尝试编写一个程序在我的Web应用程序上进行自动化测试。为此,我使用HttpURLConnection打开了一个连接。
我要测试的页面之一执行302重定向。我的测试代码如下所示:
URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
connection.connect();
system.out.println(connection.getURL().toString());
错误。
重定向永远不会发生,它会打印出原始URL。但是,如果我通过调用connection.getResponseCode()更改出connection.connect()行,则可以正常使用。
URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
//connection.connect();
connection.getResponseCode();
system.out.println(connection.getURL().toString());
为什么我会看到这种行为?我做错什么了吗?
谢谢您的帮助。