Home > Back-end >  Curl URL into vb.net
Curl URL into vb.net

Time:10-19

Hi i'm trying to find a solution how to implement the below curl in vb.net:

curl -X 'GET' \ 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=false&include_last_updated_at=false&precision=full' \ -H 'accept: application/json'

CodePudding user response:

Dim webClient As New System.Net.WebClient

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

Dim result As String = webClient.DownloadString("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=false&include_last_updated_at=false&precision=full")

And its stored in variable result as json

CodePudding user response:

Using System.Net.Http you can do that as follows:

Private Sub GetBtcPrice()

    Using client As System.Net.Http.HttpClient = New System.Net.Http.HttpClient

        client.DefaultRequestHeaders.Add("accept", "application/json")
        Dim sb As New StringBuilder
        sb.Append("?")
        sb.Append("ids=bitcoin&")
        sb.Append("vs_currencies=usd&")
        sb.Append("include_market_cap=false&")
        sb.Append("include_24hr_vol=false&")
        sb.Append("include_24hr_change=false&")
        sb.Append("include_last_updated_at=false&")
        sb.Append("precision=full")

        Dim response As HttpResponseMessage = client.GetAsync("https://api.coingecko.com/api/v3/simple/price" & sb.ToString).GetAwaiter().GetResult()
        Dim sResponse As String = response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
        Console.WriteLine("response: " & sResponse)

    End Using


End Sub
  • Related