响应代码404使用apache commons

时间:2016-02-12 01:34:32

标签: java apache http http-status-code-404

我正在为API http://www.sptrans.com.br/desenvolvedores/APIOlhoVivo/Documentacao.aspx?1#docApi-autenticacao构建一个包装器(它是葡萄牙语,但你明白了。)

我在发出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;
    }
}

1 个答案:

答案 0 :(得分:0)

我从API文档中看到(尽管我无法读取葡萄牙语),令牌需要在URL中,而不是发布到它:

POST /Login/Autenticar?token={token}

我认为您正在将表单发布到此端点。

你应该试试这个:

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

不要拨打post.setEntity(...)