【发布时间】:2012-05-07 15:52:10
【问题描述】:
我已经搜索了一段时间,但没有找到明确的答案。我正在尝试登录网站。 https://hrlink.healthnet.com/ 该网站重定向到一个不符合要求的登录页面。我必须将我的登录凭据发布到重定向的 URL。
我正在尝试用 Java 编写代码,但我不明白如何从响应中获取 URL。它可能看起来有点乱,但我在测试时就是这样。
HttpGet httpget = new HttpGet("https://hrlink.healthnet.com/");
HttpResponse response = httpclient.execute(httpget);HttpEntity entity = response.getEntity();
String redirectURL = "";
for(org.apache.http.Header header : response.getHeaders("Location")) {
redirectURL += "Location: " + header.getValue()) + "\r\n";
}
InputStream is;
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
我知道我被重定向了,因为我的结果字符串显示为实际的登录页面,但我无法获取新 URL。
在 FireFox 中,我使用 TamperData。当我导航到这个网站https://hrlink.healthnet.com/ 时,我有一个带有 302 的 GET - Found 和登录页面的位置。然后另一个 GET 到实际的登录页面
非常感谢您的帮助。
【问题讨论】:
标签: java httpclient httpresponse http-get