java 避免重定向,使用HttpClient 4.1.1避免循环重定向

How can I avoid circular redirect using HttpClient 4.1.1. As I am getting the error like this:-

executing requestGET http://home.somehost.com/Mynet/pages/cHome.xhtml HTTP/1.1

org.apache.http.client.ClientProtocolException

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)

at edu.uci.ics.crawler4j.url.WebURL.setURL(WebURL.java:122)

at edu.uci.ics.crawler4j.crawler.CrawlController.addSeed(CrawlController.java:207)

at edu.uci.ics.crawler4j.example.advanced.Controller.main(Controller.java:31)

Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://home.somehost.com/Mynet/pages/Home.xhtml'

at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:168)

at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:193)

at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:1021)

at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:482)

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)

This is my code...

DefaultHttpClient client = null;

try

{

// Set url

//URI uri = new URI(url.toString());

client = new DefaultHttpClient();

client.getCredentialsProvider().setCredentials(

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),

new UsernamePasswordCredentials("test", "test"));

URL url1 = new URL (url);

HttpURLConnection connection = (HttpURLConnection) url1.openConnection();

connection.setFollowRedirects(false);

HttpGet request = new HttpGet(url);

final HttpParams params = new BasicHttpParams();

HttpClientParams.setRedirecting(params, false);

HttpContext context = new BasicHttpContext();

System.out.println("----------------------------------------");

System.out.println("executing request" + request.getRequestLine());

HttpResponse response = client.execute(request, context);

HttpEntity entity = response.getEntity();

System.out.println(response.getStatusLine());

InputStream content = entity.getContent();

BufferedReader in =

new BufferedReader (new InputStreamReader (content));

String line;

while ((line = in.readLine()) != null) {

// System.out.println(line);

}

} catch(Exception e) {

e.printStackTrace();

}

解决方案

You can set the ClientPNames.ALLOW_CIRCULAR_REDIRECTS to true, this will allow redirects to the same location.

client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

See more info here

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值