The connection to URL timed out after 5.000 seconds. API URL timeout error

7 visualizaciones (últimos 30 días)
Hii everyone. How are you guys?
I created the code below in order to analyse api url links of criptoccurrency and present bitcoin values from different currencies in the same graph.
clear all
close all
clc
num_exec = 600; %Número de repetições (300 = + ou - 5 minutos)
for c=1:num_exec
%Mercado Bitcoin
httpsUrlmb = "https://www.mercadobitcoin.net/api/BTC/ticker/";
dataUrlmb = strcat(httpsUrlmb);
datamb = webread(dataUrlmb);
timemb = [datamb.ticker.date];
valuemb = [datamb.ticker.last];
valuemb2 = str2double(valuemb);
value_arraymb(c) = valuemb2;
time_arraymb(c) = timemb; %deixar o contador no eixo x até descobrir forma de trabalhar com o tempo no formato unix
%Bitblue
httpsUrlbb = "https://bitblue.com/api/stats";
dataUrlbb = strcat(httpsUrlbb);
databb = webread(httpsUrlbb);
idx1 = strfind(databb,'"last_price":')
idx1 = idx1 + 12;
idx2 = strfind(databb,',"last_transaction_type":')
idx2 = idx2 - 1;
cbb = idx2 - idx1;
for cbb=1:cbb
valuebb(cbb) = databb(idx1+cbb);
end
valuebb2 = str2double(valuebb);
value_arraybb(c) = valuebb2;
%Novadax
httpsUrlnd = "https://api.novadax.com/v1/market/ticker?symbol=BTC_BRL";
dataUrlnd = strcat(httpsUrlnd);
datand = webread(dataUrlnd);
% timend = [datand.data.timestamp];
valuend = [datand.data.lastPrice];
valuend2 = str2double(valuend);
value_arraynd(c) = valuend2;
% time_arraynd(c) = timend; %deixar o contador no eixo x até descobrir forma de trabalhar com o tempo no formato unix
%Walltime
httpsUrlwt = "https://s3.amazonaws.com/data-production-walltime-info/production/dynamic/walltime-info.json?now=1528962473468.679.0000000000873";
dataUrlwt = strcat(httpsUrlwt);
datawt = webread(dataUrlwt);
idxwt1 = strfind(datawt,'"last_inexact":"')
idxwt1 = idxwt1 + 16;
idxwt2 = strfind(datawt,'"last":"')
idxwt2 = idxwt2 - 8;
cwt = idxwt2 - idxwt1;
for cwt=1:cwt+1
valuewt(cwt) = datawt(idxwt1+cwt-1);
end
valuewt2 = str2double(valuewt);
value_arraywt(c) = valuewt2;
%Coinext
httpsUrlcn = "https://apex.coinext.com.br/tickers";
dataUrlcn = strcat(httpsUrlcn);
datacn = webread(dataUrlcn);
valuecn = [datacn.x1.last];
% valuecn2 = str2double(valuecn);
value_arraycn(c) = valuecn;
%Foxbit
httpsUrlfb = "https://watcher.foxbit.com.br/api/Ticker?exchange=Foxbit&Pair=BRLXBTC";
dataUrlfb = strcat(httpsUrlfb);
datafb = webread(dataUrlfb);
valuefb = [datafb.last];
value_arrayfb(c) = valuefb;
%Plot
contador(c) = c;
plot(contador,value_arraymb,'r-*',contador,value_arraybb,'b-*',contador,value_arraynd,'g-*',contador,value_arraywt,'m-*',contador,value_arraycn,'c-*',contador,value_arrayfb,'y-*')
axis([0 contador(c) valuemb2-10000 valuemb2+10000]) %substituir axis contador por tempo quando resolver problema do tempo unix
xlabel("Tempo")
ylabel("Valor")
title("Valor do bitcoin (R$)");
grid on
legend("Mercado Bitcoin","Bitblue","Novadax","Walltime","Coinext","Foxbit","Bitcointrade")
% pause(1)
%Encerramento da automação
drawnow;
clc
end
I have a error message
Error using webread (line 122)
The connection to URL 'https://apex.coinext.com.br/tickers' timed out after 5.000 seconds. The reason is
"Waiting for response header". Perhaps the server is not responding or weboptions.Timeout needs to be set to a
higher value.
I know that this is internet connection issue, but I want my code to ignore this coneection when it was not able to reach the site. I want it to ignore and go to the next api url link. For those values that the code was not able to reach it because of the internet bad quality connection, I want the code to repeat it the last value for the respective vector.
Please help me! Have a lovely day!

Respuestas (1)

Chetan
Chetan el 29 de Sept. de 2023
I understand that you are trying to get the data from the API and want to continue if the connection request timed out.
You can achieve this by using the “try” and “catch” statements and in the catch, you can take the previous successful data.
Refer to the following MathWorks documentation for the use of the try-catch block:
Here is the sample code for the same:
clear all
close all
clc
num_exec = 60; % Number of repetitions (1 = +/- 5 minutes)
% Initialize variables
value_array = zeros(1, num_exec); % Array to store Bitcoin values
for c = 1:num_exec
try
% Send a request to the CoinGecko API to retrieve the Bitcoin value in BRL
data = webread('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=BRL');
% Extract the Bitcoin value from the API response
value = data.bitcoin.brl;
% Store the Bitcoin value in the value_array at index c
value_array(c) = value;
catch
% In case of a connection timeout or any other error, repeat the last value
if c > 1
value_array(c) = value_array(c-1);
end
end
% Plot the value_array up to the current repetition
contador = 1:c;
plot(contador, value_array(1:c), 'r-*')
% Update the plot axis limits and labels
axis([0 num_exec value_array(c)-10000 value_array(c)+10000])
xlabel("Time")
ylabel("Value")
title("Bitcoin Value (BRL)")
grid on
% Display the plot
drawnow;
% Clear the command window
clc
end
I hope these suggestions help you resolve the issue you are facing.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by