Skip to content

Commit 596bf9c

Browse files
committed
updating java file
1 parent 168ed62 commit 596bf9c

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

java/Search/BingCustomSearchv7.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*Copyright (c) Microsoft Corporation. All rights reserved.
22
Licensed under the MIT License.*/
3-
3+
// <imports>
4+
import javax.net.ssl.HttpsURLConnection;
45
import java.io.InputStream;
56
import java.net.URL;
67
import java.net.URLEncoder;
@@ -9,25 +10,27 @@
910
import java.util.Map;
1011
import java.util.Scanner;
1112

12-
import javax.net.ssl.HttpsURLConnection;
13-
1413
import com.google.gson.Gson;
1514
import com.google.gson.GsonBuilder;
1615
import com.google.gson.JsonObject;
1716
import com.google.gson.JsonParser;
17+
// </imports>
1818

19-
public class CustomSrchJava {
19+
public class Bing {
2020

21-
// Add your Bing Custom Search endpoint to your environment variables.
21+
// <vars>
22+
// Add your Bing Custom Search endpoint and key to your environment variables.
23+
// Your endpoint will have the form:
24+
// https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
2225
static String host = System.getenv("BING_CUSTOM_SEARCH_ENDPOINT");
23-
static String path = "/bingcustomsearch/v7.0/search";
24-
// Add your Bing Custom Search subscription key to your environment variables.
2526
static String subscriptionKey = System.getenv("BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY");
26-
static String customConfigId = "YOUR-CUSTOM-CONFIG-ID";
27-
27+
28+
static String path = "/search";
29+
static String customConfigId = "YOUR-CUSTOM-CONFIG-ID"; //you can also use "1"
2830
static String searchTerm = "Microsoft"; // Replace with search term specific to your defined sources.
29-
30-
public static SearchResults SearchImages (String searchQuery) throws Exception {
31+
// </vars>
32+
// <searchWeb>
33+
public static SearchResults SearchWeb(String searchQuery) throws Exception {
3134
// construct URL of search request (endpoint + query string)
3235
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchTerm, "UTF-8") + "&CustomConfig=" + customConfigId);
3336
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
@@ -52,15 +55,17 @@ public static SearchResults SearchImages (String searchQuery) throws Exception {
5255
stream.close();
5356
return results;
5457
}
55-
58+
// </searchWeb>
59+
// <prettify>
5660
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
5761
public static String prettify(String json_text) {
5862
JsonParser parser = new JsonParser();
5963
JsonObject json = parser.parse(json_text).getAsJsonObject();
6064
Gson gson = new GsonBuilder().setPrettyPrinting().create();
6165
return gson.toJson(json);
6266
}
63-
67+
// </prettify>
68+
// <main>
6469
public static void main (String[] args) {
6570
if (subscriptionKey.length() != 32) {
6671
System.out.println("Invalid Bing Search API subscription key!");
@@ -71,7 +76,7 @@ public static void main (String[] args) {
7176
try {
7277
System.out.println("Searching the Web for: " + searchTerm);
7378

74-
SearchResults result = SearchImages(searchTerm);
79+
SearchResults result = SearchWeb(searchTerm);
7580

7681
System.out.println("\nRelevant HTTP Headers:\n");
7782
for (String header : result.relevantHeaders.keySet())
@@ -85,8 +90,13 @@ public static void main (String[] args) {
8590
System.exit(1);
8691
}
8792
}
93+
// </main>
8894
}
8995

96+
// <searchResultsClass>
97+
// put this in a seperate .java file.
98+
import java.util.HashMap;
99+
90100
// Container class for search results encapsulates relevant headers and JSON data
91101
class SearchResults{
92102
HashMap<String, String> relevantHeaders;
@@ -97,3 +107,5 @@ class SearchResults{
97107
}
98108

99109
}
110+
// </searchResultsClass>
111+

0 commit comments

Comments
 (0)