Mugoma J. Okomba
Mugoma J. Okomba

Reputation: 3295

Avoiding HTTP Error 301 when using HttpClient 4.x

This question relates to Migrate from Commons HttpClient to HttpComponents Client (migrating from HttpClient 3 to HttpClient 4).

I can access a link physically in a browser but when I try to access same link using HttpClient 4.1.13 I get HTTP error 301 ("Move Permanent").

When accessing same link using HttpClient 3.1 I don't get HTTP error 301.

What could be causing such anormally? Could it be a setting that I am missing under HC 4 that makes it behave that way?

Upvotes: 0

Views: 4405

Answers (3)

ok2c
ok2c

Reputation: 27538

This can be happening because the origin server(s) respond differently to requests with different User-Agent header.

Upvotes: 2

Ryan Stewart
Ryan Stewart

Reputation: 128829

First, 301 isn't an "error". The 3xx responses are "redirection" responses. 4xx and 5xx are the error series.

In response to your question, per RFC 2616, a user agent may not automatically handle redirects if the request method isn't GET or HEAD. I'd guess you're doing a POST. HttpClient 4 is more strict in its compliance to the HTTP spec than its predecessor, and it definitely complies with the spec on this point, so that's probably why you're seeing this problem.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718788

If you are using the HC 4.x HttpClient service, it should be dealing with redirects automatically. At least, that's what the documentation says. (I note that there are some configuration properties, but the documentation says that automatic redirection handling is enabled by default.)

Upvotes: 0

Related Questions