【问题标题】:Rest API for Blockchain.info用于 Blockchain.info 的 Rest API
【发布时间】:2019-02-21 09:22:43
【问题描述】:

我正在使用 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.Task1.GetResultCore(Boolean waitCompletionNotification) +12846467 System.Threading.Tasks.Task1.get_Result() +33

【问题讨论】:

    标签: c# rest blockchain.info-api


    【解决方案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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这个错误实际上是在告诉你问题所在......

      无法创建 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 语句中,因为您目前没有处理它)

      【讨论】:

      • 但我使用的是 SSL 证书
      • 不,你不是。您的 URL 以 HTTP 开头...您需要 HTTPS
      • 我使用 https 还是有同样的问题
      猜你喜欢
      • 2014-03-17
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多