Установка обработчика заголовков http:http-client

Я хотел бы установить заголовок User-Agent при использовании процессора: http-client

Я вижу, что есть опция mappedRequestHeaders, но как использовать для установки User-Agent: например, Mozilla

Либо я что-то упускаю, либо у spring-xd отсутствует процессор: header-enricher. Это сделает то, что я ищу в обычном контексте Spring Integration:

<int:chain id="mychain" input-channel="http-request-data">  
    <int:header-enricher>  
        <int:header name="User-Agent" value="curl/7.0.48"/>    
    </int:header-enricher>
    <int-http:outbound-gateway url="${url}"  http-method="GET" expected-response type="java.lang.String"/>      
    <int-file:outbound-channel-adapter directory="${output-folder}" filename-generator-expression="'${filename}'" />
</int:chain> 

person vladsfl    schedule 29.10.2014    source источник


Ответы (2)


<int-http:outbound-gateway> имеет атрибут request-factory и может внедрить в него HttpComponentsClientHttpRequestFactory bean-компонент. Этот ClientHttpRequestFactory предоставляет некоторое значение заголовка User-Agent по умолчанию.

Поскольку это ваш собственный экземпляр Spring XD, вы можете изменить существующий [XD_HOME]\xd\modules\processor\http-client\config\http-client.xml, добавив это определение компонента:

<bean id="requestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

<int-http:outbound-gateway request-factory="requestFactory"/>

Оставив все остальное без изменений.

Кроме того, вы должны добавить org.apache.httpcomponents:httpclient:4.3.5 jar к lib этого http-client модуля.

Commons HttpClient позволяет даже указать системное свойство httpclient.useragent для переопределения значения по умолчанию.

Не стесняйтесь поднимать (https://jira.spring.io/browse/XD) проблему относительно processor:header-enricher.

Конечно, вы можете преодолеть это прямо сейчас с помощью процессора Groovy script:

org.springframework.integration.support.MessageBuilder.withPayload(payload)
     .copyHeaders(headers)
     .setHeader('User-Agent', 'curl/7.0.48')
     .build()

ХТН

person Artem Bilan    schedule 30.10.2014

Подход Groovy со сценарием хорош. Сейчас я добавил процессор/header.xml : Подход со сценарием Groovy хорош. Сейчас я добавил процессор/header.xml - очень просто, мне нужен только один заголовок

<channel id="input"/>

<header-enricher input-channel="input" output-channel="output">
    <header name="${name}" value="${value}"/>
</header-enricher>

<channel id="output"/>
person vladsfl    schedule 30.10.2014