使用 Commons HttpClient 3.x 時,需手動處理重導向(自動重導向會陷入循環或失去 Cookie)。
GetMethod get = new GetMethod(url);
get.setFollowRedirects(false); // 關閉自動重導向
client.executeMethod(get);
Header loc = get.getResponseHeader("location");
if (loc != null) {
String redirectUrl = loc.getValue();
// 遞迴或迴圈呼叫 doRequest(redirectUrl, ...)
// 注意攜帶原請求的 Cookie/Header
}
get.releaseConnection();
建議:專案若非維舊,升級至 **HttpClient 4.5+ / 5.x**,內建 `LaxRedirectStrategy`、`DefaultRedirectStrategy` 可配置,代碼更簡潔。