Bitcoin Forum
May 08, 2024, 07:18:37 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Can't get API response  (Read 149 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
February 13, 2022, 05:27:34 AM
 #1

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
1715152717
Hero Member
*
Offline Offline

Posts: 1715152717

View Profile Personal Message (Offline)

Ignore
1715152717
Reply with quote  #2

1715152717
Report to moderator
The Bitcoin network protocol was designed to be extremely flexible. It can be used to create timed transactions, escrow transactions, multi-signature transactions, etc. The current features of the client only hint at what will be possible in the future.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1367


View Profile
February 13, 2022, 12:22:42 PM
Merited by ABCbits (3)
 #2

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;
    }
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
February 13, 2022, 02:47:36 PM
 #3

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 Smiley ) 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(
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6730


bitcoincleanup.com / bitmixlist.org


View Profile WWW
February 16, 2022, 05:41:35 PM
Merited by ABCbits (1), hugeblack (1)
 #4

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).

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
stanner.austin
Member
**
Offline Offline

Activity: 67
Merit: 53


View Profile
February 17, 2022, 08:03:51 AM
 #5

@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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!