|
| 1 | +//Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +//Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.IO; |
| 7 | +using System.Collections.Generic; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | + |
| 10 | +namespace BingSearchApisQuickstart |
| 11 | +{ |
| 12 | + |
| 13 | + class Program |
| 14 | + { |
| 15 | + |
| 16 | + // Replace this string value with your valid access key. |
| 17 | + const string subscriptionKey = "Enter your subscription key here"; |
| 18 | + |
| 19 | + // Verify the endpoint URI. If you encounter unexpected authorization errors, |
| 20 | + // double-check this value against the Bing search endpoint in your Azure dashboard. |
| 21 | + const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/images/search"; |
| 22 | + |
| 23 | + const string searchTerm = "tropical ocean"; |
| 24 | + |
| 25 | + // A struct to return image search results seperately from headers |
| 26 | + struct SearchResult |
| 27 | + { |
| 28 | + public string jsonResult; |
| 29 | + public Dictionary<String, String> relevantHeaders; |
| 30 | + } |
| 31 | + |
| 32 | + static void Main() |
| 33 | + { |
| 34 | + Console.OutputEncoding = System.Text.Encoding.UTF8; |
| 35 | + |
| 36 | + Console.WriteLine("Searching images for: " + searchTerm + "\n"); |
| 37 | + //send a search request using the search term |
| 38 | + SearchResult result = BingImageSearch(searchTerm); |
| 39 | + //deserialize the JSON response from the Bing Image Search API |
| 40 | + dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult); |
| 41 | + |
| 42 | + var firstJsonObj = jsonObj["value"][0]; |
| 43 | + Console.WriteLine("Title for the first image result: " + firstJsonObj["name"]+"\n"); |
| 44 | + //After running the application, copy the output URL into a browser to see the image. |
| 45 | + Console.WriteLine("URL for the first image result: " + firstJsonObj["webSearchUrl"]+"\n"); |
| 46 | + |
| 47 | + Console.Write("\nPress Enter to exit "); |
| 48 | + Console.ReadLine(); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Performs a Bing Image search and return the results as a SearchResult. |
| 53 | + /// </summary> |
| 54 | + static SearchResult BingImageSearch(string searchQuery) |
| 55 | + { |
| 56 | + // Construct the URI of the search request |
| 57 | + var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery); |
| 58 | + |
| 59 | + // Perform the Web request and get the response |
| 60 | + WebRequest request = WebRequest.Create(uriQuery); |
| 61 | + request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey; |
| 62 | + HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result; |
| 63 | + string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); |
| 64 | + |
| 65 | + // Create result object for return |
| 66 | + var searchResult = new SearchResult() |
| 67 | + { |
| 68 | + jsonResult = json, |
| 69 | + relevantHeaders = new Dictionary<String, String>() |
| 70 | + }; |
| 71 | + |
| 72 | + // Extract Bing HTTP headers |
| 73 | + foreach (String header in response.Headers) |
| 74 | + { |
| 75 | + if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-")) |
| 76 | + searchResult.relevantHeaders[header] = response.Headers[header]; |
| 77 | + } |
| 78 | + |
| 79 | + return searchResult; |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | +} |
0 commit comments