hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From caffeine zombie <nicholas.hammo...@gmail.com>
Subject cannot chanhge execute method for single HttpClient instance
Date Tue, 10 Feb 2009 12:25:15 GMT

Hi,
I'm using HttpClient to talk to ruby on rails webservice.  Basically after
much playing about I have discovered that in order to post into the
interface I need to supply an authenticity_token in the parameters of the
POST.  Now in order to get a valid authenticity_token I first need to do a
GET on the relevant interface.  The authenticity_token is only valid for a
particular HttpClient session, so should be simple enough to do a GET then a
post ... wrong.  If I use the same HttpClient and c executrMethod with a
getMethod and then a postMethod, the getMethod just gets executed twice. 
Here is my code, is there a way to override the getMethod so the postMethod
gets done correctly

many thanks


HttpClient client = new HttpClient();
	    client.getParams().setParameter("http.useragent", "Test Client");
	    client.getParams().setParameter("http.connection.timeout",new
Integer(5000));

String authToken = null;
	    try {

	      method.setURI(new
URI("http://dev-integration-test.dsi.thus.net:3001/orders/new", true));
	      int returnCode = client.executeMethod(method);

	      if(returnCode != HttpStatus.SC_OK) {
	        System.err.println(
	          "Unable to fetch default page, status code: " + returnCode);
	      }
	      BufferedInputStream is = new
BufferedInputStream(method.getResponseBodyAsStream());
	      authToken = XhtmlParser.parseAuthToken(is);
	      logger.debug("XXXXXXXXXXXXX authToken is: " + authToken );

	    } catch (HttpException he) {
	      System.err.println(he);
	    } catch (IOException ie) {
	      System.err.println(ie);
	    } finally {
	      method.releaseConnection();
	    }

client.getParams().setParameter("http.useragent", "Test Client");

	    BufferedReader br = null;

	    method = new
PostMethod("http://dev-integration-test.dsi.thus.net:3001/orders/new");
	    System.out.println("defined postmethod");
	    ((PostMethod)method).addParameter("authenticity_token", authToken);
	    
	    
	    
	    try{
	      int returnCode = client.executeMethod(method);
	    
	      if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
	        System.err.println("The Post method is not implemented by this
URI");
	        // still consume the response body
	   
	      } else {
	    	  logger.debug("post response is " +
method.getResponseBodyAsString()); # XXXXX this comes out the same as stream
I got back from the original GET call
	        
	  ..
-- 
View this message in context: http://www.nabble.com/cannot-chanhge-execute-method-for-single-HttpClient-instance-tp21932530p21932530.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message