Getting URL after a redirect using HttpClient.Execute(HttpGet)

前端 未结 1 1093
遥遥无期
遥遥无期 2020-12-11 04:30

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 tha

相关标签:
1条回答
  • 2020-12-11 05:22

    Check out w3c documentation:

    10.3.3 302 Found

    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).

    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.

    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);
    
      ... ...
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题