HTTPConnection不返回重定向页面

时间:2016-11-07 15:09:48

标签: jsp redirect response httpconnection

我有一个执行某些功能的JSP页面,如果一切顺利,将重定向到成功页面。如果出现错误,页面将继续并显示错误信息。我正在编写一个测试来驱动页面但是来自服务器的响应始终是错误信息天气或不起作用。该页面被重定向并在浏览器中正常工作。我正在使用Eclipse和Tomcat 8。

我尝试在重定向之前添加response.flushBuffer()和response.reset()无效。

这是JSP页面:

<%
    boolean success = performFunction();
    if (success)
    {
        response.sendRedirect(“SuccessPage”);
        return;
    }
%>

<html>
<h1>There was an error</h1>
<html>

这是我用来发布到页面的代码:

    StringBuffer pageResp = new StringBuffer();

    // Setup a URL object
    URL url = new URL(urlString);


    // Open a connection to the URL
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setUseCaches(false);
    httpConn.setAllowUserInteraction(false);
    httpConn.setRequestMethod("POST");
    httpConn.setReadTimeout(10000);
    httpConn.setConnectTimeout(15000);


    // Complete the request by getting the input stream
    BufferedReader is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
    int len = 0;
    char[] buf = new char[2048];
    while ((len=is.read(buf,0,2048)) != -1)
        pageResp.append(new String(buf).substring(0, len));

    is.close();

    System.out.print(pageResp.toString());

0 个答案:

没有答案