0

我有一堆用 Apache Jersey 客户端实现的自动化测试用例。除了我针对 RESTful Web 服务删除文件的一种情况外,一切都运行良好。该错误会发生一段时间,并且无法每次都重现,但确实会发生。

我用于删除文件的代码使用 Jersey 非常简单直观

Client client = Client.create();
WebResource resource = client.resource(uri);
ClientResponse response = resource.delete(ClientResponse.class);

上面的代码正在删除由 uri 指定的服务器上的文件,这是我得到“服务器重定向太多次 (20)”错误的唯一情况。它有时会过去,但有时会失败。我正在寻找更多线索以开始调查。

我得到的例外是:

com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: Server redirected too many  times (20)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
com.sun.jersey.api.client.Client.handle(Client.java:648)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
com.sun.jersey.api.client.WebResource.delete(WebResource.java:272)
<more my user exceptions are skipped here...>

Caused by: java.net.ProtocolException: Server redirected too many  times (20)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1446)
java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:240) com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
com.sun.jersey.api.client.Client.handle(Client.java:648)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
com.sun.jersey.api.client.WebResource.delete(WebResource.java:272)

希望有人能给点提示吗?谢谢。

4

1 回答 1

1

Looks like the server is sending repeated redirects for some reason.

Call client.setFollowRedirects(false); on your Jersey client instance to avoid the client getting into a redirect loop.

于 2012-06-19T14:11:45.790 回答