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 Mon, 13 Feb 2012 16:25:32 GMT
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


Mime
View raw message