我在访问odata服务时遇到麻烦,因为它需要特定的标头,而且我无法直接在ODataClient上添加它,有人对如何解决我的问题有任何想法吗?
伪代码:
ODataClient client = ODataClientFactory.getClient();
client.addHeader("Header","123456789"); // <---- this code is what I am seeking
URI customersUri = client.newURIBuilder("uri/northwindmodel.svc")
.appendEntitySetSegment("Customers").build();
ODataRetrieveResponse<ODataEntitySetIterator<ODataEntitySet, ODataEntity>> response
= client.getRetrieveRequestFactory().getEntitySetIteratorRequest(customersUri).execute();
依赖性:
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>odata-client-core</artifactId>
<version>4.5.0</version>
</dependency>
答案 0 :(得分:1)
ODataClient
没有API来设置其他标头,可能是因为它直接不直接拥有HttpClient
。它拥有我的AbstractODataRequest
类,因此您有API可以为每个HTTP请求设置附加标头。
您可以在addCustomHeader
中使用ODataRequest
方法。
例如您可以通过以下方式重构示例代码以实现目标。
ODataClient client = ODataClientFactory.getClient();
// client.addHeader("Header","123456789"); // <---- this code is what I am seeking
URI customersUri = client.newURIBuilder("uri/northwindmodel.svc")
.appendEntitySetSegment("Customers").build();
ODataEntitySetIteratorRequest<ClientEntitySet, ClientEntity> entitySetIteratorRequest =
client.getRetrieveRequestFactory().getEntitySetIteratorRequest(qryUri);
entitySetIteratorRequest.addCustomHeader("Custom-Header-key", "Custom-Header-Value");
ODataRetrieveResponse<ClientEntitySetIterator<ClientEntitySet, ClientEntity>> response = entitySetIteratorRequest.execute();