下面列出了怎么用org.apache.http.client.CircularRedirectException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Executes HEAD request to get redirects.
* Throws IllegalStateException in case of status code outside of "200-207"
* @param from URI to issue HEAD request
* @return List of redirects. {@code null} if there are no redirects.
*/
public List<URI> getRedirects(URI from)
{
try
{
HttpClientContext httpContext = HttpClientContext.create();
httpClient.doHttpHead(from, httpContext).verifyStatusCodeInRange(HttpStatus.SC_OK,
HttpStatus.SC_MULTI_STATUS);
return httpContext.getRedirectLocations();
}
catch (IOException | ExternalServiceException e)
{
String exceptionMsg;
if (e.getCause() instanceof CircularRedirectException)
{
exceptionMsg = e.getCause().getMessage() + " Circular redirects are forbidden by default. "
+ "To allow them, please set property "
+ "'http.redirects-provider.circular-redirects-allowed=true'";
}
else
{
exceptionMsg = e.getMessage();
}
throw new IllegalStateException(exceptionMsg, e);
}
}
@Test
public void shouldUpdateErrorMessageAndRethrowAsIllegalStateExceptionInCaseOfCircularRedirectException()
throws IOException
{
ClientProtocolException clientProtocolException = new ClientProtocolException(
new CircularRedirectException("Circular reference"));
when(httpClient.doHttpHead(URI_EXAMPLES, httpClientContext)).thenThrow(clientProtocolException);
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class,
() -> redirectsProvider.getRedirects(URI_EXAMPLES));
assertEquals("Circular reference Circular redirects are forbidden by default. To allow them, please set "
+ "property 'http.redirects-provider.circular-redirects-allowed=true'",
illegalStateException.getMessage());
}