hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kra...@darkfluid.com
Subject 501 Method not implemented with successive POST requests
Date Wed, 11 Aug 2010 19:02:54 GMT
  Hello HttpComponents-Users,

I'm facing a strange issue when using HttpComponents with successive 
POST requests against a simple Tomcat servlet (Form-based POST 
Authentication is enabled). After a sucessfull authentication, the 
client requests the same protected resource a few times in a row, but 
the second request always returns a "501 Method not implemented" status. 
All other requests succeed .

The following communication takes place:
1. Client requests a protected resource; Tomcat returns the login page 
2. Client provides the user credentials and calls the auth check page; 
If successful Tomcat returns a redirect (HTTP302)
3. Client sets the location from the redirect for the next 
request(identical to URI from #1) and requests it; Tomcat returns the 
page (HTTP200)
4. Client requests the same page again; Tomcat returns a HTTP501 - 
Method not implemented
5. Client requests the same page again; Tomcat returns the page (HTTP200)
6. Client requests the same page again; Tomcat returns the page (HTTP200)
7. Client requests the same page again; Tomcat returns the page (HTTP200)

The log message regarding the 501 says: '"testName=testValuePOST 
/test/secure/ HTTP/1.1" 501 1278', which may indicate that the entity 
and the HTTP-method name somehow got mixed up!? Strangely the HTTP501 
does not appear in case of a JSP for the requested resource, only if 
facing a form-based POST authentication Servlet in Tomcat (tested with 
Tomcat v5.5 and v7). The servlet does only implement post and get(which 
calls the doPost) and returns just a string. If it helps I provided the 
zipped webapp dir along with the client code as a attachment.

The source code for my client:
     DefaultHttpClient httpclient = new DefaultHttpClient();
     HttpPost httpost = new HttpPost(new 
     List <NameValuePair> nvps = new ArrayList <NameValuePair>();
     nvps.add(new BasicNameValuePair("j_username", "tomcat"));
     nvps.add(new BasicNameValuePair("j_password", "tomcat"));
     httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
     HttpResponse response = httpclient.execute(httpost);
     System.out.println("Response Status: " + response.getStatusLine());

     response = httpclient.execute(httpost);
     HttpEntity entity = response.getEntity();
     System.out.println("Response Status: " + response.getStatusLine());
     int code = response.getStatusLine().getStatusCode()/100;
     if(code == 3) {
       String location = response.getFirstHeader("location").getValue();
       System.out.println("==> REDIRECT to "+location);
       httpost.setURI(new URI(location));
       List <NameValuePair> params = new ArrayList <NameValuePair>();
       params.add(new BasicNameValuePair("testName", "testValue"));
       httpost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
       for(int i=0; i<5; i++) {
         response = httpclient.execute(httpost);
         System.out.println("Response Status: " + response.getStatusLine());
         entity = response.getEntity();

I hope I can solve this issue. It could be a problem with Tomcat, though.

Thanks for helping in advance
best regards

View raw message