hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Example of modifying post parameters in a request interceptor
Date Tue, 14 Feb 2012 11:38:11 GMT
On Mon, 2012-02-13 at 13:38 -0800, Warren Bell wrote:
> Oleg,
> 
> Is there a better place in HttpPost or HttpClient to modify post form
> parameters or should they just be modified before HttpPost or HttpClient
> ? That sounds like a dumb question, but I like the idea of an
> interceptor to modify the request before it gets sent.
> 
> Thanks,
> 

Generally I would avoid modifying the request URI in mid-air so to speak
as too many things directly depend on its properties (cookie and
connection management for instance). But as long as you only intend to
modify query parameters only I guess doing it from a request interceptor
should be all right.

Cheers

Oleg


> Warren Bell
> 
> On 2/13/12 8:25 AM, Oleg Kalnichevski wrote:
> > On Sun, Feb 12, 2012 at 12:11:04PM -0800, Warren Bell wrote:
> >> How do I write a request interceptor that removes, modifies or adds post
> >> parameters to the request? Everything I have tried so far throws
> >> exceptions that state you are not able to add a parameter at this point.
> >>
> > 
> > I am not entirely sure it is a good idea to modify request uris from an interceptor,
but this is how you could do it.
> > 
> > ---
> > DefaultHttpClient httpclient = new DefaultHttpClient();
> > httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
> >     
> >     public void process(
> >             final HttpRequest request, 
> >             final HttpContext context) throws HttpException, IOException {
> >         RequestWrapper wrapper = (RequestWrapper) request;
> >         URIBuilder uribuilder = new URIBuilder(wrapper.getURI());
> >         uribuilder.setParameter("myparam", "stuff");
> >         try {
> >             wrapper.setURI(uribuilder.build());
> >         } catch (URISyntaxException ex) {
> >             throw new HttpException("Invalid request URI", ex);
> >         }
> >     }
> > });
> > HttpResponse response = httpclient.execute(new HttpGet("http://somehost/"));
> > EntityUtils.consumeQuietly(response.getEntity());
> > ---
> > 
> > Oleg
> > 
> > 
> > 
> >> -- 
> >> Thanks,
> >>
> >> Warren Bell
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> >> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >>
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 



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