获取错误:无法创建SSL/TLS安全通道

本文关键字:TLS 安全 通道 SSL 创建 取错误 获取 | 更新日期: 2023-09-27 18:03:37

我正在创建用于搜索图像的Shutterstock web应用程序。看一下代码

  function createImageComponent(image) {
      var wrapper = $('<div>');
      var thumbWrapper = $('<div>');
      var thumbnail = $('<img>');
      var description = $('<p>');
      $(thumbnail).attr('src', image.assets.preview.url);
      $(thumbWrapper)
        .addClass('thumbnail-wrapper')
        .css('height', image.assets.)
        .css('width', image.assets.preview.width)
        .append(thumbnail);
      $(description)
        .text(image.description)
        .attr('title', image.description);
      $(wrapper)
        .addClass('media-wrapper image')
        .append(thumbWrapper)
        .append(description);
      return wrapper;
    }
    // Create video wrapper component
  function createVideoComponent(video) {
      var wrapper = $('<div>');
      var preview = $('<video>');
      var description = $('<p>');
      $(preview)
        .attr('src', video.assets.thumb_mp4.url)
        .attr('controls', true)
        .attr('autoplay', true);
      $(description)
        .text(video.description)
        .attr('title', video.description);
      $(wrapper)
        .addClass('media-wrapper video')
        .append(preview)
        .append(description);
      return wrapper;
    }
    // Search media by type
  function search(opts, media_type) {
      var $container = $('#' + media_type + '-search-results');
      var createComponentFunc = media_type === 'image' ? createImageComponent : createVideoComponent;
      // Get Client ID and Client Secret for use in the Authorization header
      var clientId = $('input[name=client_id]').val();
      var clientSecret = $('input[name=client_secret]').val();
      var jqxhr = $.ajax({
          url: 'https://api.shutterstock.com/v2/' + media_type + 's/search',
          data: opts,
          headers: {
            // Base 64 encode 'client_id:client_secret'
            Authorization: 'Basic ' + window.btoa(clientId + ':' + clientSecret)
          }
        })
        .done(function(data) {
          if (data.total_count === 0) {
            $container.append('<p>No Results</p>');
            return;
          }
          $.each(data.data, function(i, item) {
            var component = createComponentFunc(item);
            $container.append(component);
          });
        })
        .fail(function(xhr, status, err) {
          alert('Failed to retrieve ' + media_type + ' search results:'n' + JSON.stringify(xhr.responseJSON, null, 2));
        });
      return jqxhr;
    }
    // On Page Load
  $(function() {
    $('#search-form').submit(function(e) {
      e.preventDefault();
      // Clear current media results
      $('#image-search-results, #video-search-results').empty();
      // Serialize form options
      var opts = $("input[value != '']", this).serialize();
      // Search and display images
      search(opts, 'image');
      // Search and display videos
      search(opts, 'video');
      return false;
    });
  });

此代码用于使用shutterstock基本身份验证搜索图像。我想用c#创建它。

,我使用下面的代码:

    LoadHttpPageWithBasicAuthentication(@"https://api.shutterstock.com/v2/images/232713811?view=full", "ClientID", "Clientsecrate");
  private string LoadHttpPageWithBasicAuthentication(string url, string username, string password)
    {
        Uri myUri = new Uri(url);
        WebRequest myWebRequest = HttpWebRequest.Create(myUri);
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
        NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
        CredentialCache myCredentialCache = new CredentialCache();
        myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
        myHttpWebRequest.PreAuthenticate = true;
        myHttpWebRequest.Credentials = myCredentialCache;
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        WebResponse myWebResponse = myWebRequest.GetResponse();
        Stream responseStream = myWebResponse.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
        string pageContent = myStreamReader.ReadToEnd();
        responseStream.Close();
        myWebResponse.Close();
        return pageContent;
    }

但是我得到了错误

请求被终止:Could not create SSL/TLS安全通道。

请帮助解决这个问题的错误

获取错误:无法创建SSL/TLS安全通道

我的shutterstock API也是这样。试着把这段代码

 try
        {
            var request = (HttpWebRequest)WebRequest.Create("https://api.shutterstock.com/v2/images/232713811?view=full");
            var username = "YourClientID";
            var password = "YourClientSecrate";
            string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
            request.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
            request.UserAgent = "MyApp 1.0";
            var response = (HttpWebResponse)request.GetResponse();
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();
              //  Image myojb = (Image)js.Deserialize(objText, typeof(Image));             
                var myojb = JsonConvert.DeserializeObject<RootObject>(objText);

                // Response.Write(reader.ReadToEnd());
            }
        }
        catch (WebException ea)    
        {
            Console.WriteLine(ea.Message);
            using (var stream = ea.Response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }

您需要Deserialize响应数据作为您想要的结果。