0

I am creating Shutterstock web application for searching images. take a look on the code

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

This code is for searching images using shutterstock basic authentication.I want to create it using c#.

and i am using the following code :

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

But i am getting the error

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

Please help what is the error stuck with this problem.

Gitz
  • 810
  • 1
  • 17
  • 48
  • @stuartd sorry sir !! I'll remove that part :) – Gitz Jul 20 '15 at 10:38
  • @stuartd i have already put these code put still getting the same problem – Gitz Jul 20 '15 at 10:53
  • So the server certificate is definitely valid? – stuartd Jul 20 '15 at 13:12
  • See [this answer](http://stackoverflow.com/questions/10822509/the-request-was-aborted-could-not-create-ssl-tls-secure-channel). You may need to use a different security protocol (SSL3 may be disallowed), and verify that your certificate is valid. – Anthony Hilyard Jul 20 '15 at 14:52
  • Enable System.Net tracing, check other questions about the same exception: http://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel, http://stackoverflow.com/questions/10822509/the-request-was-aborted-could-not-create-ssl-tls-secure-channel – CodeCaster Jul 22 '15 at 10:42
  • @CodeCaster Sir i had already viewed and try the code you suggested but still getting the same error – Gitz Jul 22 '15 at 10:44
  • That is not in your question. Add exactly what you tried. Why didn't you respond to the tracing part of my comment, didn't you understand? See [MSDN: How to: Configure Network Tracing](https://msdn.microsoft.com/en-us/library/ty48b824(v=vs.110).aspx). – CodeCaster Jul 22 '15 at 10:46
  • @CodeCaster I had applied the code of two links you suggest. I am trying to configure Network tracing as you suggest the link .. will let you know after its done. And I have change the question title also :) – Gitz Jul 22 '15 at 10:52
  • Don't expect copy-pasteable code from SO. You need to understand and solve the problem. How to troubleshoot the actual source of this issue is explained in those links. – CodeCaster Jul 22 '15 at 10:53
  • @CodeCaster Yes Sir !! I know :) actually this link is also provide by Anthony on above comments and i had already read the answers and applied but its not works for me – Gitz Jul 22 '15 at 10:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83966/discussion-between-gitz-and-codecaster). – Gitz Jul 22 '15 at 11:31
  • @CodeCaster i have update the Network tracing on Web config and the "SSL" error is resolved but now getting the error "The remote server returned an error: (400) Bad Request." – Gitz Jul 22 '15 at 12:14

1 Answers1

0

This was happening for me also for the shutterstock API .try to put this code

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

You need to Deserialize the response data as what result you want.

Rajeev Mehta
  • 820
  • 2
  • 10
  • 32