使用HttpClient.Execute(HttpGet)重定向后获取URL [英] Getting URL after a redirect using HttpClient.Execute(HttpGet)

查看:694
本文介绍了使用HttpClient.Execute(HttpGet)重定向后获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一段时间,但没有找到明确的答案.我正在尝试登录网站. https://hrlink.healthnet.com/ 该网站重定向到不合法的登录页面.我必须将我的登录凭据发布到重定向的URL.

I have searched for a while and I am not finding a clear answer. I am trying to log into a webstie. https://hrlink.healthnet.com/ This website redirects to a login page that is not consitent. I have to post my login credentials to the redirected URL.

我正在尝试用Java编写此代码,但我不明白如何从响应中获取URL.它可能看起来有些混乱,但是我在测试时就用这种方式.

Im am trying to code this in Java but I do not understand how to get the URL from the response. It may look a bit messy but I have it this way while I am testing.

    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.

I know i get redirected because my result string shows be the actual login page but I am not able to get the new URL.

在FireFox中,我正在使用TamperData.当我导航到该网站 https://hrlink.healthnet.com/时,我有一个GET-302-找到并找到了登录页面.然后另一个GET到实际的登录页面

In FireFox I am using TamperData. When I navigate to this website https://hrlink.healthnet.com/ I have a GET with a 302 - Found and the Location of the Login Page. Then another GET to the actual Login Page

非常感谢您的帮助.

推荐答案

查看 w3c文档:

10.3.3 302找到

10.3.3 302 Found

临时URI应该由响应中的Location字段给出.除非请求方法是HEAD,否则响应的实体应包含简短的超文本注释,并带有指向新URI的超链接.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

如果响应GET或HEAD以外的请求而收到302状态码,则用户代理不得自动重定向请求,除非用户可以确认,因为这可能会改变请求被请求的条件.发布.

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

一种解决方案是使用POST方法来中断客户端的自动重定向:

One solution is to use POST method to break auto-redirecting at client side:

HttpPost request1 = new HttpPost("https://hrlink.healthnet.com/");
HttpResponse response1 = httpclient.execute(request1);

// expect a 302 response.
if (response1.getStatusLine().getStatusCode() == 302) {
  String redirectURL = response1.getFirstHeader("Location").getValue();
  
  // no auto-redirecting at client side, need manual send the request.
  HttpGet request2 = new HttpGet(redirectURL);
  HttpResponse response2 = httpclient.execute(request2);

  ... ...
}

希望这会有所帮助.

这篇关于使用HttpClient.Execute(HttpGet)重定向后获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆