Balu Sidhartha
Balu Sidhartha

Reputation: 71

Rest API for Blockchain.info

I am working with asp.net and i am accessing blockchain.info api for getting the bitcoin current rate and i was using the flowing method for getting the same

public string BtcToDollar(decimal btc)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://blockchain.com/");
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));
        string methodename = "frombtc?currency=USD&value=" + HttpUtility.HtmlEncode(btc * 100000000) ;
        var response = client.GetAsync(methodename);
        return response.Result.Content.ReadAsStringAsync().Result;

     }

this was working fine but now I am getting erorr

Server Error in '/' Application. The request was aborted: Could not create SSL/TLS secure channel. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

Stack Trace:

[WebException: The request was aborted: Could not create SSL/TLS secure channel.] System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +64

[HttpRequestException: An error occurred while sending the request.]

[AggregateException: One or more errors occurred.] System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +4324957 System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) +12846467 System.Threading.Tasks.Task1.get_Result() +33

Upvotes: 0

Views: 712

Answers (2)

Naim Jamalludin
Naim Jamalludin

Reputation: 195

Try this instead

public string BtcToDollar(decimal btc)
{
    using (HttpClient client = new HttpClient())
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolTypeExtensions.Tls11 | SecurityProtocolTypeExtensions.Tls12;
        client.BaseAddress = new Uri("https://blockchain.com/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        string methodename = "frombtc?currency=USD&value=" + HttpUtility.HtmlEncode(btc * 100000000);
        var response = client.GetAsync(methodename);
       return response.Result.Content.ReadAsStringAsync().Result;
    }
}

Upvotes: 1

Aydin
Aydin

Reputation: 15294

The error is literally telling you the problem...

Could not create SSL/TLS secure channel

Meaning you're not using HTTPS scheme.

public string BtcToDollar(decimal btc)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://blockchain.com/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        string methodename = "frombtc?currency=USD&value=" + HttpUtility.HtmlEncode(btc * 100000000);
        var response = client.GetAsync(methodename);
        return response.Result.Content.ReadAsStringAsync().Result;
    }
}

Try that! ^ (also wrapped client in using statement, as you're currently not disposing of it)

Upvotes: 0

Related Questions