0

我正在使用 asp.net,我正在访问 blockchain.info api 以获取比特币当前汇率,我正在使用流动方法来获得相同的汇率

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;

     }

这工作正常,但现在我越来越错误

“/”应用程序中的服务器错误。请求被中止:无法创建 SSL/TLS 安全通道。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Net.WebException:请求已中止:无法创建 SSL/TLS 安全通道。

堆栈跟踪:

[WebException:请求被中止:无法创建 SSL/TLS 安全通道。] System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +64

[HttpRequestException:发送请求时发生错误。]

[AggregateException:发生一个或多个错误。] System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +4324957 System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) +12846467 System.Threading.Tasks.Task1.get_Result() +33

4

2 回答 2

1

试试这个

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;
    }
}
于 2018-11-24T05:03:23.897 回答
0

该错误实际上是在告诉您问题所在...

无法创建 SSL/TLS 安全通道

这意味着您没有使用HTTPS方案。

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;
    }
}

试试看!^(也将客户端包装在 using 语句中,因为您目前没有处理它)

于 2018-10-08T02:31:30.813 回答