0

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?

Community
  • 1
  • 1
Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37

3 Answers3

2

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1st I need to correct myself. I meant HTTP response but wrote HTTP error instead. So, the issue is about avoiding HTTP 301 responses for cases where the link is accessible. Secondly, I am using GET and not POST. – Mugoma J. Okomba May 06 '12 at 06:23
  • Well, the redirect handling is pretty simple. If you just debug it and break at the beginning of the handleResponse method of [DefaultRequestDirector](https://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.1.3/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java), you'll be able to see exactly why it's not following the redirect. – Ryan Stewart May 06 '12 at 07:31
  • Actually it's not that I want the redirect. I just wonder why the redirect? I can access the same link physically (in a browser) and using HC 3. Why do I get HTTP 301 under HC 4? – Mugoma J. Okomba May 06 '12 at 11:42
  • Most likely because HC 3 and browsers are following the redirect automatically. If you were to monitor the actual network traffic, you'd see your first request gets a 301, and a second request is made automatically. If that's not true, then something else is going on, and you'd need to inspect the different requests and responses to see if you can figure out what's causing the difference. – Ryan Stewart May 07 '12 at 03:06
2

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

ok2c
  • 26,450
  • 5
  • 63
  • 71
  • This could be a probable cause. How do I change User-Agent header in HC 4 so that I don't get 301 responses? Kind of like make User-Agent in HC4 same as that in HC 3 (or normal browser). I tried context.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)") but this didn't help. – Mugoma J. Okomba May 06 '12 at 18:19
  • DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(AllClientPNames.USER_AGENT, "whatever"); – ok2c May 07 '12 at 10:24
  • httpClient.getParams().setParameter(AllClientPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"). That did the magic. Many Thanks. – Mugoma J. Okomba May 08 '12 at 10:23
0

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216