-
Notifications
You must be signed in to change notification settings - Fork 265
/
BingNewsSearchv7.cs
63 lines (50 loc) · 2.46 KB
/
BingNewsSearchv7.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Newtonsoft.Json;
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Generic;
/* This sample makes a call to the Bing News Search API with a query word and returns related news.
* Bing News Search API:
* https://dev.cognitive.microsoft.com/docs/services/e5e22123c5d24f1081f63af1548defa1/operations/56b449fbcf5ff81038d15cdf
*/
namespace BingNewsSearch
{
class Program
{
// Add your Azure Bing Search V7 subscription key to your environment variables.
static string accessKey = Environment.GetEnvironmentVariable("BING_SEARCH_V7_SUBSCRIPTION_KEY");
// Add your Azure Bing Search V7 endpoint to your environment variables.
static string endpoint = Environment.GetEnvironmentVariable("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/news/search";
const string query = "Microsoft";
static void Main()
{
// Create dictionary to store a few extracted headers
Dictionary<String, String> relevantHeaders = new Dictionary<String, String>();
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Searching news for: " + query);
// Construct the URI of the search request
var uriQuery = endpoint + "?q=" + Uri.EscapeDataString(query);
// Perform the Web request and get the response
WebRequest request = HttpWebRequest.Create(uriQuery);
request.Headers["Ocp-Apim-Subscription-Key"] = accessKey;
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
// Extract Bing HTTP headers
foreach (String header in response.Headers)
{
if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-"))
relevantHeaders[header] = response.Headers[header];
}
Console.WriteLine("\nRelevant HTTP Headers:\n");
foreach (var header in relevantHeaders)
Console.WriteLine(header.Key + ": " + header.Value);
Console.WriteLine("\nJSON Response:\n");
dynamic parsedJson = JsonConvert.DeserializeObject(json);
Console.WriteLine(JsonConvert.SerializeObject(parsedJson, Formatting.Indented));
Console.ReadLine();
}
}
}