2

I develop a Java class which contain a method open a URL connection to RESTFUL and get data. If I run it as Java application (main function), it run good, server return code 200 OK with data returned. But when I try to call that method in another spring bean @service, it return 403 error. Same code, same class but different way to call and different http code return.

Is there anyone see it before? Any help will be very appreciated. Thanks all.

UPDATE: my code

public static String call(String urlStr, String USER_AGENT, String TRUSTED_THIRDPARTY, String PRIVATE_HASH) throws Exception {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();

    String headerString = "";

    connection.setRequestProperty("Accept", "*,*");
    headerString += "accept: *,*\n";
    connection.setRequestProperty("User-Agent", USER_AGENT);
    headerString += "user-agent: " + USER_AGENT + "\n";
    connection.setRequestProperty("X-Trusted-Third-Party", TRUSTED_THIRDPARTY);
    headerString += "x-trusted-third-party: " + TRUSTED_THIRDPARTY + "\n";

    String message = urlStr + ":" + headerString + ":";
    String hash = base64sha256(message, PRIVATE_HASH);

    connection.setRequestProperty("X-hash-authen", hash);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String decodedString = "";
    while ((decodedString = in.readLine()) != null) {
        System.out.println(decodedString);
    }
    in.close();

    return decodedString;
}
Quan M Le
  • 73
  • 2
  • 8

0 Answers0