浏览器不会使用HttpClient LaxRedirectStrategy重定向

时间:2017-01-17 11:10:49

标签: java http-post httpclient http-redirect

我试图发布一个http请求帖子,但我似乎无法弄清楚为什么我的浏览器在设置重定向策略LaxRedirectStrategy后仍然无法重定向。我在这里错过了什么吗?

maven httpcomponents

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String restEndpoint = "http://localhost:8080/res-auth/ep/login/post";
    if (request != null) {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            CloseableHttpClient httpClient = HttpClientBuilder.create()
                    .setRedirectStrategy(new LaxRedirectStrategy()).build();
            JSONObject jsonUser = new JSONObject();
            jsonUser.put("email", username);
            jsonUser.put("password", password);
            jsonUser.put("rememberMe", false);
            jsonUser.put("domainHome", domainHome);
            jsonUser.put("loginHome", loginHome);
            jsonUser.put("userHome", userHome);
            jsonUser.put("callback", userHome);

            StringEntity input = new StringEntity(jsonUser.toString());
            HttpPost postRequest = new HttpPost(restEndpoint);
            postRequest.setEntity(input);
            postRequest.setHeader("content-type", "application/json");
            postRequest.setHeader("Accept", "application/json");
            postRequest.setHeader("User-Agent", USER_AGENT);

            CloseableHttpResponse restResponse = httpClient.execute(postRequest);

            System.out.println("Response code :"
                    + restResponse.getStatusLine().getStatusCode());
            try {
                final HttpEntity entity = restResponse.getEntity();
                if (entity != null) {
                    final InputStream instream = entity.getContent();
                    instream.close();
                }
            } finally {
                restResponse.close();
            }

        } catch (JSONException ex) {
            Logger.getLogger(HttpClientServletTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        request.setAttribute("error", "Unknown User please try again");
        request.getRequestDispatcher("/WEB-INF/login.xhtml").forward(request, response);
    }
}

这里我的servlet发出请求帖子

@POST
@Path("post")
@Consumes({MediaType.APPLICATION_JSON})
@Transactional
public Response authenticateUser(String json) throws IOException {
    JSONObject jsonObject = new JSONObject(json);
    setEmail(jsonObject.get("email").toString());
    setPassword(jsonObject.get("password").toString());
    setCallback(jsonObject.get("callback").toString());
    setDomainHome(jsonObject.get("domainHome").toString());
    setRememberMe(jsonObject.getBoolean("rememberMe"));

    login(getEmail(), getPassword(), isRememberMe());
    URI uri = new URI(getCallback());
    return Response.seeOther(uri).build();
}

其余的api正在调用

c:\PROGRA~1\DEBUGG~1\cdb.exe -z    "C:\Progra~1\BitNam~1\apache2\htdocs\dumps\dumpfile.dmp" -lines -c ".logopen C:\Progra~1\BitNam~1\apache2\htdocs\dumps\temp\ministack_manual.txt; !sym noisy;.reload;!analyze -v;.ecxr;!for_each_frame dv /t;.logclose;

0 个答案:

没有答案