Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Sanka555 on February 13, 2022, 05:27:34 AM



Title: Can't get API response
Post by: Sanka555 on February 13, 2022, 05:27:34 AM
Quote
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
I'm trying to send a simple request:

Quote
protected static void checkBalances() throws JSONException, IOException, InterruptedException {
        StringBuilder urlBuilder = new StringBuilder("https://blockscout.com/etc/mainnet/api?module=account&action=balance&address=0x37802776D2A1654C8888da63757e0Df4440DA79B");
                JSONObject results = readJsonFromUrl(urlBuilder.toString());
        System.out.println("*"+results+"*");
        System.exit(1);
       
    }
 
    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            return new JSONObject(jsonText);
        } finally {
            is.close();
        }
    }
 
    protected static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

flies in response
Quote
java.io.IOException: Server returned HTTP response code: 403 for URL: https://blockscout.com/etc/mai... f4440DA79B
at sun.net.http://www.protocol.http.HttpU... .java:1900)
at sun.net.http://www.protocol.http.HttpU... .java:1498)
at sun.net.http://www.protocol.https.Http... l.java:268)
at java.net.URL.openStream(URL.java:1067)
at Main.readJsonFromUrl(Main.java:352)
......


the same request in the browser gives
Quote
{"message":"OK","result":"840524398469120","status":"1"}
what am i doing wrong please tell me


Title: Re: Can't get API response
Post by: PawGo on February 13, 2022, 12:22:42 PM
Maybe it would be better if you create a class which corresponds to result and then you convert result to your local class.
Or use more abstract approach, like casting to Map. See how I check balance using blockchain.com api:
https://github.com/PawelGorny/lostword/blob/master/src/main/java/com/pawelgorny/lostword/WorkerUnknownCheckAll.java


Code:
private static long checkBalance(List<String> addresses) throws IOException, InterruptedException {
        String urlbalance = "https://blockchain.info/balance?active=";
        BufferedReader in;
        URL url = new URL(urlbalance + Joiner.on(",").join(addresses));
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String json="";
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json += inputLine;
        }
        boolean x = false;
        Map<String, LinkedTreeMap> addressBalance = GSON.fromJson(json, Map.class);
        long final_balance = 0L;
        for (Map.Entry<String, LinkedTreeMap> entry:addressBalance.entrySet()){
            final_balance = ((Double)entry.getValue().get("final_balance")).longValue();
            if (final_balance>0){
                if (!x){
                    System.out.println("");
                }
                System.out.println(Utils.SPACE_JOINER.join(addresses)+" "+entry.getKey()+" "+final_balance);
                x = true;
            }
        }
        Thread.sleep(2000);
        return final_balance;
    }


Title: Re: Can't get API response
Post by: Sanka555 on February 13, 2022, 02:47:36 PM
Maybe it would be better if you create a class which corresponds to result and then you convert result to your local class.
Or use more abstract approach, like casting to Map. See how I check balance using blockchain.com api:
https://github.com/PawelGorny/lostword/blob/master/src/main/java/com/pawelgorny/lostword/WorkerUnknownCheckAll.java


Code:
private static long checkBalance(List<String> addresses) throws IOException, InterruptedException {
        String urlbalance = "https://blockchain.info/balance?active=";
        BufferedReader in;
        URL url = new URL(urlbalance + Joiner.on(",").join(addresses));
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String json="";
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json += inputLine;
        }
        boolean x = false;
        Map<String, LinkedTreeMap> addressBalance = GSON.fromJson(json, Map.class);
        long final_balance = 0L;
        for (Map.Entry<String, LinkedTreeMap> entry:addressBalance.entrySet()){
            final_balance = ((Double)entry.getValue().get("final_balance")).longValue();
            if (final_balance>0){
                if (!x){
                    System.out.println("");
                }
                System.out.println(Utils.SPACE_JOINER.join(addresses)+" "+entry.getKey()+" "+final_balance);
                x = true;
            }
        }
        Thread.sleep(2000);
        return final_balance;
    }

Thank you. You have a great program. (I'm now looking for the missing word in your program :) ) But in this case, the problem is not in the output setting. The server gives 403. I don't even get to the output.


I think the problem is in the encoding or request settings(


Title: Re: Can't get API response
Post by: NotATether on February 16, 2022, 05:41:35 PM
Side note: OP is trying to fetch balance from an Ethereum Classic address using a 3rd party API.


I would suggest not using StringBuilder() class and use String() directly because the StringBuilder might be adding extra characters inside your URL, and the server could be returning 403 for Not Found error (some hosts are configured for that instead of using 404).


Title: Re: Can't get API response
Post by: stanner.austin on February 17, 2022, 08:03:51 AM
@Sanka555
Possible issue related to output headers, api server don't understand your request so returning you 403.
Take a look at https://cloud.google.com/appengine/docs/standard/java/issue-requests#Setting_Request_Headers