|
1 | | -//Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -//Licensed under the MIT License. |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using Newtonsoft.Json; |
4 | 5 | using System; |
5 | 6 | using System.Text; |
6 | 7 | using System.Net; |
7 | 8 | using System.IO; |
8 | 9 | using System.Collections.Generic; |
9 | 10 |
|
10 | | -namespace BingNewsSearchCSharpCore |
| 11 | +namespace BingNewsSearch |
11 | 12 | { |
12 | | - |
13 | 13 | class Program |
14 | 14 | { |
15 | | - // ********************************************** |
16 | | - // *** Update or verify the following values. *** |
17 | | - // ********************************************** |
18 | | - |
19 | 15 | // Add your Azure Bing Search V7 subscription key to your environment variables. |
20 | | - const string accessKey = Environment.GetEnvironmentVariable("BING_SEARCH_V7_SUBSCRIPTION_KEY"); |
| 16 | + static string accessKey = Environment.GetEnvironmentVariable("BING_SEARCH_V7_SUBSCRIPTION_KEY"); |
21 | 17 | // Add your Azure Bing Search V7 endpoint to your environment variables. |
22 | | - const string uriBase = Environment.GetEnvironmentVariable("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/news/search"; |
| 18 | + static string endpoint = Environment.GetEnvironmentVariable("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/news/search"; |
23 | 19 |
|
24 | | - const string searchTerm = "Microsoft"; |
25 | | - |
26 | | - // Used to return news search results including relevant headers |
27 | | - struct SearchResult |
28 | | - { |
29 | | - public String jsonResult; |
30 | | - public Dictionary<String, String> relevantHeaders; |
31 | | - } |
| 20 | + const string query = "Microsoft"; |
32 | 21 |
|
33 | 22 | static void Main() |
34 | 23 | { |
35 | | - Console.OutputEncoding = System.Text.Encoding.UTF8; |
36 | | - Console.WriteLine("Searching news for: " + searchTerm); |
| 24 | + // Create dictionary to store a few extracted headers |
| 25 | + Dictionary<String, String> relevantHeaders = new Dictionary<String, String>(); |
37 | 26 |
|
38 | | - SearchResult result = BingNewsSearch(searchTerm); |
| 27 | + Console.OutputEncoding = Encoding.UTF8; |
39 | 28 |
|
40 | | - Console.WriteLine("\nRelevant HTTP Headers:\n"); |
41 | | - foreach (var header in result.relevantHeaders) |
42 | | - Console.WriteLine(header.Key + ": " + header.Value); |
| 29 | + Console.WriteLine("Searching news for: " + query); |
43 | 30 |
|
44 | | - Console.WriteLine("\nJSON Response:\n"); |
45 | | - Console.WriteLine(JsonPrettyPrint(result.jsonResult)); |
46 | | - |
47 | | - Console.Write("\nPress Enter to exit "); |
48 | | - Console.ReadLine(); |
49 | | - } |
50 | | - |
51 | | - /// <summary> |
52 | | - /// Performs a Bing News search and return the results as a SearchResult. |
53 | | - /// </summary> |
54 | | - static SearchResult BingNewsSearch(string searchQuery) |
55 | | - { |
56 | 31 | // Construct the URI of the search request |
57 | | - var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery); |
| 32 | + var uriQuery = endpoint + "?q=" + Uri.EscapeDataString(query); |
58 | 33 |
|
59 | 34 | // Perform the Web request and get the response |
60 | 35 | WebRequest request = HttpWebRequest.Create(uriQuery); |
61 | 36 | request.Headers["Ocp-Apim-Subscription-Key"] = accessKey; |
62 | 37 | HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result; |
63 | 38 | string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); |
64 | 39 |
|
65 | | - // Create result object for return |
66 | | - var searchResult = new SearchResult(); |
67 | | - searchResult.jsonResult = json; |
68 | | - searchResult.relevantHeaders = new Dictionary<String, String>(); |
69 | | - |
70 | 40 | // Extract Bing HTTP headers |
71 | 41 | foreach (String header in response.Headers) |
72 | 42 | { |
73 | 43 | if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-")) |
74 | | - searchResult.relevantHeaders[header] = response.Headers[header]; |
| 44 | + relevantHeaders[header] = response.Headers[header]; |
75 | 45 | } |
76 | 46 |
|
77 | | - return searchResult; |
78 | | - } |
79 | | - |
80 | | - /// <summary> |
81 | | - /// Formats the given JSON string by adding line breaks and indents. |
82 | | - /// </summary> |
83 | | - /// <param name="json">The raw JSON string to format.</param> |
84 | | - /// <returns>The formatted JSON string.</returns> |
85 | | - static string JsonPrettyPrint(string json) |
86 | | - { |
87 | | - if (string.IsNullOrEmpty(json)) |
88 | | - return string.Empty; |
89 | | - |
90 | | - json = json.Replace(Environment.NewLine, "").Replace("\t", ""); |
91 | | - |
92 | | - StringBuilder sb = new StringBuilder(); |
93 | | - bool quote = false; |
94 | | - bool ignore = false; |
95 | | - int offset = 0; |
96 | | - int indentLength = 3; |
97 | | - |
98 | | - foreach (char ch in json) |
99 | | - { |
100 | | - switch (ch) |
101 | | - { |
102 | | - case '"': |
103 | | - if (!ignore) quote = !quote; |
104 | | - break; |
105 | | - case '\'': |
106 | | - if (quote) ignore = !ignore; |
107 | | - break; |
108 | | - } |
| 47 | + Console.WriteLine("\nRelevant HTTP Headers:\n"); |
| 48 | + foreach (var header in relevantHeaders) |
| 49 | + Console.WriteLine(header.Key + ": " + header.Value); |
109 | 50 |
|
110 | | - if (quote) |
111 | | - sb.Append(ch); |
112 | | - else |
113 | | - { |
114 | | - switch (ch) |
115 | | - { |
116 | | - case '{': |
117 | | - case '[': |
118 | | - sb.Append(ch); |
119 | | - sb.Append(Environment.NewLine); |
120 | | - sb.Append(new string(' ', ++offset * indentLength)); |
121 | | - break; |
122 | | - case '}': |
123 | | - case ']': |
124 | | - sb.Append(Environment.NewLine); |
125 | | - sb.Append(new string(' ', --offset * indentLength)); |
126 | | - sb.Append(ch); |
127 | | - break; |
128 | | - case ',': |
129 | | - sb.Append(ch); |
130 | | - sb.Append(Environment.NewLine); |
131 | | - sb.Append(new string(' ', offset * indentLength)); |
132 | | - break; |
133 | | - case ':': |
134 | | - sb.Append(ch); |
135 | | - sb.Append(' '); |
136 | | - break; |
137 | | - default: |
138 | | - if (ch != ' ') sb.Append(ch); |
139 | | - break; |
140 | | - } |
141 | | - } |
142 | | - } |
| 51 | + Console.WriteLine("\nJSON Response:\n"); |
| 52 | + dynamic parsedJson = JsonConvert.DeserializeObject(json); |
| 53 | + Console.WriteLine(JsonConvert.SerializeObject(parsedJson, Formatting.Indented)); |
143 | 54 |
|
144 | | - return sb.ToString().Trim(); |
| 55 | + Console.ReadLine(); |
145 | 56 | } |
146 | 57 | } |
147 | 58 | } |
0 commit comments