2013-05-13 17 views
2

我使用此代碼,它利用odata4j ODataClientRequestODataConsumer試圖調用需要身份驗證OData服務:如何添加頁眉信息ODataConsumer在odata4j?

String url = "https://mylocalhost/api/odata/People()?$filter=PID%20eq%20'10'"; 

    Map<String, String> headers = new HashMap<String, String>(); 
    headers.put("AccountID", "100"); 
    ODataClientRequest clientRequest = new ODataClientRequest("GET", url, headers, null, null); 

    ODataConsumer consumer = ODataConsumer.create(url); 

    for(OEntity entity : consumer.getEntities("People").execute()){ 

不過,我得到一個驗證錯誤,因爲服務器請求頭認證信息。我如何創建包含所需授權標頭信息的ODataConsumer

回答

0

不用手動添加標題,我相信你可以在客戶端使用基本身份驗證(因爲您收到的驗證錯誤),當您設置的消費可以增加一個內置的客戶端「的行爲」。對於BasicAuthenticationBehavior.java代碼顯示在以下鏈接:

BasicAuthenticationBehavior.java

添加的基本身份驗證行爲您ODataConsumer將類似於下面的代碼:

ODataConsumer.Builder builder = ODataConsumers.newBuilder(url); 
builder.setClientBehaviors(new BasicAuthenticationBehavior(LoginUsername, LoginPassword));  
ODataConsumer c = builder.build(); 

for(OEntity entity : c.getEntities("EntityName").execute()){ 
    System.out.println(entity.getProperty("Name").getValue().toString()); 
} 
相關問題