-2

I'm trying to get in VB.Net the updated BTC value from internet.

I'm using this method in a normal private sub.

Dim btcvalue As String = "https://blockchain.info/tobtc?currency=USD&value=1"
    Try
                Dim reply As String = client.DownloadString(btcvalue)
                TextBox1.Text = reply
          Catch
    End Try

The result is that it get nothing. I've tried to browse blockchain.info with internet explorer and infact it doesn't connect. Seems like the site is blocking that browser, that is used by default by vb.net. I can get the same value from another site but blockchain was more easy cause from that url I had extracted directly the string without putting more code. The same code I used to extract a string from a raw pastebin page worked instead. Internet explorer connect fine to pastebin site.

How can I get a precise string (btc value) from google for example?

Wario
  • 13
  • 6
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/215459/discussion-on-question-by-wario-get-updated-btc-value-from-internet). – Samuel Liew Jun 07 '20 at 10:16

1 Answers1

1

While debugging, you most likely want to add some proper exception handling. Only use empty Catch blocks in production code when you are absolutely sure you want to ignore errors (might be an idea to log the errors in any case), and always handle the most likely exceptions by explicit type.

If you add some exception handling to your code:

Dim btcvalue As String = "https://blockchain.info/tobtc?currency=USD&value=1"
Try
    Dim reply As String = client.DownloadString(btcvalue)
    TextBox1.Text = reply

Catch webEx As WebException
    MessageBox.Show(webEx.Message)
    Exit Sub    
End Try

you should see the error message:

"The request was aborted: Could not create SSL/TLS secure channel."

As you are trying to access a Https url, you would most likely want to specify a security policy using the ServicePointManager.SecurityProtocol property:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Using client As New WebClient

    Dim btcvalue As String = "https://blockchain.info/tobtc?currency=USD&value=1"
    Try
        Dim reply As String = client.DownloadString(btcvalue)
        TextBox1.Text = reply

    Catch webEx As WebException
        MessageBox.Show(webEx.Message)
        Exit Sub    
    End Try

End Using

Quite often you need to specify a User Agent. However, for this request it looks like you don't have to, but if you do, it seems it has to be an agent that refers to browsers that are more modern than IE. eg:

client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " &
                                 "AppleWebKit/537.36 (KHTML, like Gecko) " &
                                 "Chrome/42.0.2311.135 Safari/537.36 Edge/12.246")
stevec
  • 62
  • 1
  • 1
  • 5
  • thanks for your help. The fact that the problem should not be the https protocol is because if I put 'pastebin.com/raw/uUepG1iH' as address it get the string. I added client.Headers.Add though. Still having the problem, adding the catch code I get the error: Connection closed: unexpected error during a send operation – Wario Jun 07 '20 at 10:47
  • The code I posted above works for me. I get "0.00010391" as a reply from the server. Commenting out the `ServicePointManager` line results in the security error message mentioned above. As far as I can tell, 'pastebin.com/raw/uUepG1iH' does not use `Https`, so would work without having to alter the protocol type. Not sure how you can get it to work with 'pastebin.com/raw/uUepG1iH' without specifying the uri scheme. You should post the exact code you are using that is giving you your current error. – stevec Jun 07 '20 at 11:03
  • ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 --- Tls12 is marked as error, Tls not instead. – Wario Jun 07 '20 at 11:07
  • What version of Windows OS, and what version of the .NET framework are you using? – stevec Jun 07 '20 at 11:09
  • https://pastebin.com/vDs4yMbQ, project is set to .net 2.0 to expand compatibility, my os is win7 64bit – Wario Jun 07 '20 at 11:11
  • 1
    It's .NET 2.0 that is causing you grief. The server is demanding at least TLS1.1, but you'd need to target at least .NET 4.5 to have that option available in the `SecurityProtocolType Enum`. I seem to recall there being a workaround for earlier .NET versions (involving changing a registry value or somesuch), but can't remember the details. Might not even work for .NET 2.0 which is ancient by today's standards. Would need to Google it. – stevec Jun 07 '20 at 11:20
  • If you really can't move to a later version of the .NET framework, then you might want to have a look at https://stackoverflow.com/questions/41863129/how-to-enable-tls-1-2-for-api-call-in-asp-net-2-0-application for starters. I can't test this, obviously, but hopefully it will point you in the right direction. – stevec Jun 07 '20 at 11:34
  • But if I install the hotfix, the users who will use my software will have to install it too? – Wario Jun 07 '20 at 11:40
  • I just migrated to .Net framework 4.7.2 and it now works but I am now worried someone will have more difficulties on opening my application. – Wario Jun 07 '20 at 14:10
  • @Wario https://learn.microsoft.com/en-gb/archive/blogs/astebner/mailbag-what-version-of-the-net-framework-is-included-in-what-version-of-the-os says that .net 4.7.2 is installed by default with the Windows 10 April 2018 update (and later). And you can see from https://support.microsoft.com/en-gb/help/13853/windows-lifecycle-fact-sheet that this is pretty much the oldest version of Windows anyone can be on and still be within the support period. Earlier versions of Windows 10, plus Windows 7, are no longer supported. – ADyson Jun 08 '20 at 12:37
  • @Wario however to make your code work you only really need .net 4.5, so if you used that then every windows 8 and windows 10 user would be able to use your app no problem. You mentioned you're still using Windows 7 even though it's no longer supported. Anyone else also using that would need to install .net 4.5 or above, if they haven't already. But really they should just be upgrading their operating system to a supported version anyway. – ADyson Jun 08 '20 at 12:39