11//Copyright (c) Microsoft Corporation. All rights reserved.
22//Licensed under the MIT License.
3-
3+ // <using>
44using System ;
55using System . Net . Http ;
66using System . Web ;
77using Newtonsoft . Json ;
8+ // </using>
89
910namespace bing_custom_search_example_dotnet
1011{
1112 class Program
1213 {
1314 static void Main ( string [ ] args )
1415 {
15- // Add your Azure Bing Custom Search subscription key to your environment variables.
16+ // <vars>
17+ // Add your Azure Bing Custom Search subscription key and endpoint to your environment variables.
18+ // Your endpoint will have the form: https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
1619 var subscriptionKey = Environment . GetEnvironmentVariable ( "BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY" ) ;
17- var customConfigId = "YOUR-CUSTOM-CONFIG-ID" ;
18- var searchTerm = args . Length > 0 ? args [ 0 ] : "microsoft" ;
19-
20- // Add your Azure Bing Custom Search endpoint to your environment variables.
21- var url = Environment . GetEnvironmentVariable ( "BING_CUSTOM_SEARCH_ENDPOINT" ) + "/bingcustomsearch/v7.0/search?" +
22- "q=" + searchTerm +
23- "&customconfig=" + customConfigId ;
20+ var endpoint = Environment . GetEnvironmentVariable ( "BING_CUSTOM_SEARCH_ENDPOINT" ) ;
2421
22+ var customConfigId = "YOUR-CUSTOM-CONFIG-ID" ; // you can also use "1"
23+ var searchTerm = args . Length > 0 ? args [ 0 ] : "microsoft" ;
24+ // </vars>
25+ // </url>
26+ // Use your Azure Bing Custom Search endpoint to create the full request URL.
27+ var url = endpoint + "/search?" + "q=" + searchTerm + "&customconfig=" + customConfigId ;
28+ // </url>
29+ // <client>
2530 var client = new HttpClient ( ) ;
2631 client . DefaultRequestHeaders . Add ( "Ocp-Apim-Subscription-Key" , subscriptionKey ) ;
32+ // </client>
33+ // <sendRequest>
2734 var httpResponseMessage = client . GetAsync ( url ) . Result ;
2835 var responseContent = httpResponseMessage . Content . ReadAsStringAsync ( ) . Result ;
2936 BingCustomSearchResponse response = JsonConvert . DeserializeObject < BingCustomSearchResponse > ( responseContent ) ;
30-
37+ // </sendRequest>
38+ // <iterateResponse>
3139 for ( int i = 0 ; i < response . webPages . value . Length ; i ++ )
3240 {
3341 var webPage = response . webPages . value [ i ] ;
@@ -38,10 +46,11 @@ static void Main(string[] args)
3846 Console . WriteLine ( "snippet: " + webPage . snippet ) ;
3947 Console . WriteLine ( "dateLastCrawled: " + webPage . dateLastCrawled ) ;
4048 Console . WriteLine ( ) ;
41- }
49+ }
50+ //</iterateResponse>
4251 }
4352 }
44-
53+ // <repsonseClasses>
4554 public class BingCustomSearchResponse
4655 {
4756 public string _type { get ; set ; }
@@ -72,4 +81,5 @@ public class OpenGraphImage
7281 public int width { get ; set ; }
7382 public int height { get ; set ; }
7483 }
84+ // <repsonseClasses>
7585}
0 commit comments