2016-02-12 86 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。我不知道你可以“什么都不发布”。这是我第一次处理这个问题。无论如何,非常感谢! –