2015-07-20 108 views
0

我正在創建Shutterstock用於搜索圖像的Web應用程序。 採取代碼獲取錯誤:無法創建SSL/TLS安全頻道

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基本authentication.I想用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; 
    } 

但我得到的錯誤

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

請幫是什麼卡住這個問題的錯誤。

+0

@stuartd對不起,先生!我將刪除該部分:) – Gitz

+0

@stuartd我已經把這些代碼放在仍然得到相同的問題 – Gitz

+0

因此,服務器證書是絕對有效的? – stuartd

回答

1

這也是發生了我的存在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響應數據什麼導致你想要的。

+0

Yehh !!感謝它的工作正常:)我認爲問題是與我的憑據@Engineeration – Gitz

+0

是的!你是對的,'request.UserAgent =「MyApp 1.0」;'也是 –

相關問題