httpclient - 如何通过 httpClient fluent 处理重定向?

我正在使用 HttpClient fluent api 编写验收测试,但遇到了一些麻烦。

@When("^I submit delivery address and delivery time$")
public void I_submit_delivery_address_and_delivery_time() throws Throwable {

    Response response = Request
            .Post("http://localhost:9999/food2go/booking/placeOrder")
            .bodyForm(
                    param("deliveryAddressStreet1",
                            deliveryAddress.getStreet1()),
                    param("deliveryAddressStreet2",
                            deliveryAddress.getStreet2()),
                    param("deliveryTime", deliveryTime)).execute();
    content = response.returnContent();
    log.debug(content.toString());
}

当我使用后转发策略时,此代码运行良好,但当我使用重定向时会引发异常。
org.apache.http.client.HttpResponseException: Found

我想要的是获取重定向页面的内容。任何想法都值得赞赏,提前致谢。

最佳答案

HTTP specification仅在人工干预后才需要重定向实体封装方法,例如 POST 和 PUT。默认情况下 HttpClient 遵守此要求。 .

10.3 Redirection 3xx

   This class of status code indicates that further action needs to be
   taken by the user agent in order to fulfill the request.  The action
   required MAY be carried out by the user agent without interaction
   with the user if and only if the method used in the second request is
   GET or HEAD. 

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

如有必要,可以使用自定义重定向策略来放宽对自动重定向的限制
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());
    Executor exec = Executor.newInstance(client);
    String s = exec.execute(Request
            .Post("http://localhost:9999/food2go/booking/placeOrder")
            .bodyForm(...)).returnContent().asString(); 

sml - 使用 foldlfoldr 插入函数 - 探索字符串

R 使用 findOverlaps() 重叠多个 GRanges - 探索字符串

angularjs - 如何将字符串变量绑定(bind)到 datepicker 控件而不是日期对象? - 探索字符串

regex - 正则表达式量词加号和星号的区别 - 探索字符串

r - set.seed() 函数 - 设置种子的最佳值是多少? - 探索字符串

.net - 如何在 BackgroundWorker 中访问 COM 对象? - 探索字符串

glsl - GLSL 中不变和精确的关键字 - 探索字符串

sbt - 如何使用 apiMappings 链接 Java API 类? - 探索字符串

openssl - Python 请求 : . pem -> .crt + key - 探索字符串

wpf - WPF依赖属性问题 - 探索字符串

magento - 安装程序脚本给我'Wrong Entity ID' Magento 错误 - 探索字符串

php - 带有Zend框架的PHPUnit - 探索字符串

module - 在 Fortran 90 中使用公共(public)块和模块在内存方面是否存在差异 - 探索字符串

gwt - 通过单击+-图像按钮添加删除listgrid - 探索字符串

最少 4 个数字和最少 1 个字符的 RegEx - 探索字符串

utf-8 - 什么是 UTF-8Y 编码? - 探索字符串

Emacs 如何只显示文件中光标上或光标之前的行 - 探索字符串

Magento 集合迭代器 - 无法获取附加属性 - 探索字符串

Windows 8 上超高显示 (3200-1800) 上的 vim - 探索字符串

ajax - 简单的 AngularJS、AJAX 和 ASP.NET MVC 示例 - 探索字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值