...
In Camel 1.5 the following algorithm is used to determine if either GET
or POST
http HTTP method should be used:
1. Use method provided in header.
2. GET
is if query string is provided in header.
3. GET
if endpoint is configured with a query string.
4. POST
if there is data to send (body is not null).
5. GET
otherwise.
Configuring URI to call
You can set the http HTTP producer's URI directly form the endpoint URI. In the route below, Camel will call our out to the external server, oldhost
, using HTTP.
Code Block |
---|
from("direct:start") .to("http://oldhost"); |
And the equivalent spring Spring sample:
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <to uri="http://oldhost"/> </route> </camelContext> |
In Camel 1.5.1 you can override the http HTTP endpoint URI by adding a header with the key, HttpProducer.HTTP_URI
, on the message.
Code Block |
---|
from("direct:start") .setHeader(org.apache.camel.component.http.HttpProducer.HTTP_URI, constant("http://newhost")) .to("http://oldhost"); |
...
Where Constants is the class, org.apache.camel.component.http.Constants
.
...
Code Block |
---|
.setHeader("CamelHttpMethod", constant("POST")) |
And the equivalent spring Spring sample:
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <setHeader headerName="CamelHttpMethod"> <constant>POST</constant> </setHeader> <to uri="http://www.google.com"/> <to uri="mock:results"/> </route> </camelContext> |
...
There is also support for proxy authentication via the proxyUsername
and proxyPassword
options.
Using proxy settings from Java System Properties
...
Configuring charset
If you are using POST
to send data you can configure the charset
using the Exchange
property:
Code Block |
---|
exchange.setProperty(Exchange.CHARSET_NAME, "iso-8859-1"); |
Or the HttpClient httpClient options: httpClient.contentCharset=iso-8859-1
...
The sample polls the Google homepage every 10 seconds and write the page to the file message.html
:
Code Block |
---|
from("timer://foo?fixedRate=true&delay=0&period=10000") .to("http://www.google.com") .setHeader(FileComponent.HEADER_FILE_NAME, "message.html").to("file:target/google"); |
...
In this sample we have the complete URI endpoint that is just what you would have typed in a web browser. Multiple URI parameters can of course be set using the &
character as separator, just as you would in the web browser. Camel does no tricks here.
...
In the header value above notice that it should not be prefixed with ?
and you can separate parameters as usual with the &
char.
Getting the Response Code
You can get the http HTTP response code from the http HTTP component by getting the value from out the Out message header with HttpProducer.HTTP_RESPONSE_CODE
.
Code Block | ||||
---|---|---|---|---|
| ||||
Exchange exchange = template.send("http://www.google.com/search", new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setHeader(HttpProducer.QUERY, constant("hl=en&q=activemq")); } }); Message out = exchange.getOut(); int responseCode = out.getHeader(HttpProducer.HTTP_RESPONSE_CODE, Integer.class); |
...
Available as of Camel 2.0
In the route below we want to route a message that we enrich with data returned from a remote HTTP call. As we want any response from the remote server, we set the throwExceptionOnFailure
option to false
so we get any response in the AggregationStrategy
. As the code is based on an a unit test that simulates a http HTTP status code 404, there is some assertion code etc.
...
To disable cookies you can set the Http HTTP Client to ignore cookies by adding this URI option:
httpClient.cookiePolicy=ignoreCookies
...
If you need more control over the http HTTP producer you should use the HttpComponent
where you can set various classes to give you custom behavior.
Setting MaxConnectionsPerHost
The Http Component have has a org.apache.commons.httpclient.HttpConnectionManager
where you can configure various global configuration for the given component.
By global, we mean , that any endpoint the component creates has the same shared HttpConnectionManager
. So, if we want to set a different value for the max connection per host, we need to define it on the http HTTP component and not on the endpoint URI that we usually usesuse. So here comes:
First, we define the http
component in spring Spring XML. Yes, we can use the same scheme name, http
that , because otherwise Camel otherwise will auto-discover and create the component with default settings. What we need is to overrule this so we can set our options. In the sample below we set the max connection to 5 instead of the default of 2.
...