我正在测试一个Web应用程序。使用Fiddler / Httpfox,我可以看到登录到Web应用程序时,在收到200 OK响应之前,有两个302 HTTP重定向。是否可以使用Java代码观察这两个重定向?
这就是我编码的内容:
public class HttpReq {
HttpURLConnection con = null;
StringBuilder str = new StringBuilder();
BufferedReader br = null;
URL address = null;
String line = null;
HttpReq () {
try {
address = new URL("http://walhs24002v.us.oracle.com/t1mockapp1/");
con = (HttpURLConnection)address.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(60000);
con.setConnectTimeout(60000);
con.setDoOutput(true);
con.setInstanceFollowRedirects(true);
con.connect();
InputStreamReader is = new InputStreamReader(con.getInputStream());
br = new BufferedReader(is);
while((line = br.readLine()) != null)
{
str.append(line + '\n');
}
//System.out.println(str);
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
}
catch (MalformedURLException m)
{
m.printStackTrace();
}
catch (IOException i)
{
i.printStackTrace();
}
finally
{
br = null;
str = null;
con = null;
}
}
public static void main(String[] args) {
HttpReq http = new HttpReq();
}
}
程序给出输出: 200 行
那里没有惊喜。有没有办法在收到200 ok之前捕获两个302重定向?
答案 0 :(得分:0)
使用defaulthttpclient,你当然可以:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html
ClientPNames.HANDLE_REDIRECTS = 'http.protocol.handle-重定向': 定义是否应自动处理重定向。这个 参数需要java.lang.Boolean类型的值。如果是这个参数 未设置HttpClient将自动处理重定向。