2016-02-12 87 views
0

我正在爲API http://www.sptrans.com.br/desenvolvedores/APIOlhoVivo/Documentacao.aspx?1#docApi-autenticacao構建一個包裝(它是用葡萄牙語,但你明白了)。響應代碼404使用apache commons

我在發出POST請求時收到響應代碼404,我不知道爲什麼。

這是正在打印什麼:

響應代碼:404 { 「消息」: 「沒有HTTP資源發現 請求URI 'http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar' 匹配」}

public static String executePost() { 
    CloseableHttpClient client = HttpClientBuilder.create().build(); 
    String targetURL = "http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar"; 
    List<NameValuePair> urlParameters = new ArrayList<>(); 
    urlParameters.add(new BasicNameValuePair("token","3de5ce998806e0c0750b1434e17454b6490ccf0a595f3884795da34460a7e7b3")); 
    try { 
     HttpPost post = new HttpPost(targetURL); 
     post.setEntity(new UrlEncodedFormEntity(urlParameters)); 

     HttpResponse response = client.execute(post); 
     System.out.println("Response Code : " 
       + response.getStatusLine().getStatusCode()); 

     BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer result = new StringBuffer(); 
     String line = ""; 
     while ((line = rd.readLine()) != null) result.append(line); 

     System.out.println(result.toString()); 
     return result.toString(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

回答

0

它從API文檔(儘管我不能讀葡萄牙語)看起來是這樣的,令牌需要在URL中,而不是在其上發佈:

POST /Login/Autenticar?token={token}

我認爲您正在向此端點發布表單。

你應該試試這個:

String targetURL = "http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar?token=3de5ce998806e0c0750b1434e17454b6490ccf0a595f3884795da34460a7e7b3"; 

別叫post.setEntity(...)

+0

就是這樣!我雖然post方法的重點是完全不使用url。我不知道你可以「什麼都不發佈」。這是我第一次處理這個問題。無論如何,非常感謝! –