HttpClient 配合 Servlet 测试应用

GetMethod 默认是开启   followRedirects(true);的 , 所以不管服务器是使用sendRedirect 还是 forward , 客户端都会执行到跳转的页面。

如果手动设置followRedirects(false) , 则sendRedirect()的页面 只能通过getMethod.getHeader("location").getValue()来获得 , 执行getMethod.getResponseAsString()得到的是空字符串。

如果手动设置followRedirects(false) , 则forward的页面 , 通过getMethod.getResponseAsString()就可以得到,getMethod.getHeader("location")返回null 。

这也就是forward也sendRedirect的区别。

 

总结一点就是sendRedirect的就是通过getMethod.getHeader("location").getValue()来取得 , 而

forward的跳转是发生在服务器内部的 , 不管你怎么设置 , 都会执行到那个页面。

 

通常用postMethod提交之后获得Header("location").getValue();是服务器使用了sendRedirect , 而如果

Header("location")为空的时候 , 是服务器使用了forward , 但是不管是那种方法 , 服务器都会在检查用户名密码之后

将登陆成功与否设置值在session中 , 对应httpclient就是有这么个cookie , 没登陆成功就是没这个cookie.

但是我们无需理会cookie的管理 , 因为可以使用httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY)

让HttpClient使用类似浏览器的方式管理cookie,这也使得httpclient的使用更为简单。

你可能感兴趣的:(应用服务器,servlet,浏览器)