Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Brayhan en 3 Diciembre 2022, 18:52 pm



Título: Necesito obtener el precio del BTC y agregarlo a TextBox
Publicado por: Brayhan en 3 Diciembre 2022, 18:52 pm
Buenas. Soy nuevo por acá, y estoy intentando hacer un sistema que me saque precios y porcentajes ya que trabajo con minería.

Básicamente necesito saber si hay alguna forma de obtener el precio del Bitcoin (no importa si no es en tiempo real, puede ser cada 5 minutos, etc) y que esa información se muestre dentro de un TextBox. Puedo introducir ese valor manualmente y el programa funciona perfectamente porque ya tengo todo lo demás listo, pero me gustaría saber si se puede automatizar de alguna manera.

Muchas gracias :D


Título: Re: Necesito obtener el precio del BTC y agregarlo a TextBox
Publicado por: AlbertoBSD en 4 Diciembre 2022, 05:29 am
Tal vez el sigueinte codigo te ayude.

Código
  1. Dim request As HttpWebRequest
  2. Dim response As HttpWebResponse = Nothing
  3. Dim reader As StreamReader
  4.  
  5. ' Set the URL of the API endpoint
  6. Dim url As String = "https://api.coindesk.com/v1/bpi/currentprice.json"
  7.  
  8. Try
  9.    ' Create a new HTTP to=python
  10. request = HttpWebRequest.Create(url)
  11.  
  12. ' Send the request and get the response
  13. response = request.GetResponse()
  14.  
  15. ' Read the response and store it in a string
  16. Dim data As String = response.GetResponseStream()
  17. Dim content As String = reader.ReadToEnd()
  18.  
  19. ' Close the response
  20. response.Close()
  21.  
  22. ' Parse the JSON response to get the current Bitcoin price
  23. Dim jObject As JObject = JObject.Parse(content)
  24. Dim price As Double = jObject("bpi")("USD")("rate_float")
  25.  
  26. ' Update the text box with the current Bitcoin price
  27. TextBox1.Text = price
  28.  
  29. Catch ex As Exception
  30.    ' Show an error message if the request failed
  31.    MessageBox.Show("Error: " & ex.Message)
  32. End Try
  33.  

Saludos!


Título: Re: Necesito obtener el precio del BTC y agregarlo a TextBox
Publicado por: Brayhan en 4 Diciembre 2022, 07:25 am
Muchas gracias por responder!

Lamentablemente no logro hacerlo funcionar :(

Me da el siguiente error:

"Dim data As String = response.GetResponseStream()" El valor de tipo Stream no se puede convertir en String.

"Dim jObject As JObject = jObject.Parse(content)" No está definido el tipo JObject. La variable jObject se usa antes de que se le haya asignado un valor.