2016-05-13 49 views
0

我對JBoss的它調用另一個休息web服務託管一個Java休息API:調用其他API

@GET 
@Path("/testDeployment") 
@Produces(MediaType.TEXT_PLAIN) 
public String testDeployment() { 
     URL url = new URL(restURL); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoOutput(true); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Authorization", "Bearer "+sessionId); 

     System.out.println("sessionId>>>> "+sessionId); 
     System.out.println("restURL>>>> "+restURL); 
     BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream()))); 

     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
      response += output; 
     } 

     conn.disconnect(); 
} 

但我得到錯誤

服務器返回的HTTP響應代碼:401網址:https://cs5.salesforce.com/services/apexrest/event/search?type=init

13:16:08738 ERROR [stderr的](默認任務-26)產生java.io.IOException:服務器返回的HTTP響應代碼:401爲URL:https://cs5.salesforce.com/services/apexrest/event/search?type=init

13:16:08747 ERROR [stderr的](默認任務-26)在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)

+0

我使用相同的代碼來調用使用java類的外部web服務,它運行良好 –

回答

0

我也加入了認證頭:

conn.setRequestProperty( 「授權」, 「承載」 +的sessionId);

但它看起來像頭需要格式化,我更新了線爲:

conn.setRequestProperty( 「授權」,的String.format( 「承載%S」 的sessionId));

和它的工作,我想在網上它需要格式化和Java應用程序上面的代碼工作得很好

2

以下是自定義的提取物的Http Status Code Definitions,這可能會幫助您解決問題:

401未授權

請求需要用戶驗證。響應必須包含一個WWW-Authenticate頭域(14.47節),其中包含一個適用於所請求資源的挑戰。客戶端可以用適當的授權標題字段重複請求(14.8節)。如果請求已包含授權憑證,則401響應表明授權已被拒絕。如果401響應包含與先前響應相同的挑戰,並且用戶代理至少已經嘗試過一次認證,則用戶應該被呈現給響應中給出的實體,因爲該實體可能包含相關的診斷信息。 HTTP訪問身份驗證頁中的 「HTTP驗證:基本和摘要訪問認證」

0

什麼都URL你打的授權,所以必須使用標頭的授權,那麼你將得到作爲

1輸出,如果你正在使用的球衣然後如下使用語法: -

try{ 
Client client = Client.create(); 
WebResource webResource = client.resource("http://localhost:8085/getStepsCount"); 
webResource.setProperty("Content-Type", "application/json;charset=UTF-8"); 
String authorization = "PASTE_KEY_HERE"; 
webResource.setProperty("Authorization", authorization); 
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); 
queryParams.add("json", js); 
String jsonString = webResource.get(String.class);}catch (Exception e){System.out.println (e.getMessage());} 

,如果你使用的是彈簧安置控制器然後使用這一個.......

@RequestMapping(value = "/getStepsUsingPostRequest", method = RequestMethod.POST) 
public ResponseEntity<Object> getDiscountUsingPost(
     @RequestBody MemberStepsDto memberStepsDto) { 
    try{ 
    final String uri = "http://localhost:8085/getStepsCount"; 
    RestTemplate restTemplate = new RestTemplate(); 
    System.out.println("starting......."); 
    String json = "{\"memberId\": \""+memberStepsDto.getMemberId()+"\",\"startDate\": \""+memberStepsDto.getStartDate()+"\",\"endDate\": \""+memberStepsDto.getEndDate()+"\"}"; 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.APPLICATION_JSON); 
    HttpEntity<String> entity = new HttpEntity<String>(json,headers); 

    String answer = restTemplate.postForObject(uri, entity, String.class); 
    System.out.println("String : " + answer); 
    } 
    catch(Exception ex){ 
     ex.printStackTrace(); 
    } 
    return new ResponseEntity<Object>(new String("Fetched Successfully"),HttpStatus.OK); 
}