其余API客户端调用HTTP PATH失败

时间:2019-02-27 10:18:03

标签: rest api path client

我可以用GET方法调用rest API,但我不知道用PATH方法调用rest API,对我有帮助。

这是我的GET方法代码:

try {
    System.setProperty("https.proxyHost", "proxyvn.net");
    System.setProperty("https.proxyPort", "8080");
    URL url = new URL("https://vn.custhelp.com/services/rest/connect/v1.4/accounts");

    URLConnection connection = url.openConnection();

    String encodedCredentials = new String(org.apache.commons.codec.binary.Base64
            .encodeBase64(org.apache.commons.codec.binary.StringUtils.getBytesUtf8("myAccount:myPass")));

    connection.setRequestProperty("Accept", "application/json");
    connection.addRequestProperty("Authorization", "Basic "+encodedCredentials);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("OSvC-CREST-Application-Context", "a");
    connection.setConnectTimeout(30000);
    connection.setReadTimeout(100000);
    connection.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    StringBuilder sb = new StringBuilder();
       while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
       System.out.println(sb.toString());
    System.out.println("\nREST Service Invoked Successfully..");
    reader.close();
} catch (Exception e) {
    System.out.println("\nError while calling REST Service");
    System.out.println(e);
}

0 个答案:

没有答案