-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingAutosuggestv7.java
More file actions
81 lines (68 loc) · 2.66 KB
/
BingAutosuggestv7.java
File metadata and controls
81 lines (68 loc) · 2.66 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.*/
import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.HttpsURLConnection;
/*
* Gson: https://github.com/google/gson
* Maven info:
* groupId: com.google.code.gson
* artifactId: gson
* version: 2.8.1
*
* Once you have compiled or downloaded gson-2.8.1.jar, assuming you have placed it in the
* same folder as this file (Autosuggest.java), you can compile and run this program at
* the command line as follows.
*
* javac Autosuggest.java -classpath .;gson-2.8.1.jar -encoding UTF-8
* java -cp .;gson-2.8.1.jar Autosuggest
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Autosuggest {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Add your Bing Autosuggest subscription key to your environment variables.
static String subscriptionKey = System.getenv("BING_AUTOSUGGEST_SUBSCRIPTION_KEY");
static String host = System.getenv("BING_AUTOSUGGEST_ENDPOINT");
static String path = "/bing/v7.0/Suggestions";
static String mkt = "en-US";
static String query = "sail";
public static String get_suggestions () throws Exception {
String encoded_query = URLEncoder.encode (query, "UTF-8");
String params = "?mkt=" + mkt + "&q=" + encoded_query;
URL url = new URL (host + path + params);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setDoOutput(true);
StringBuilder response = new StringBuilder ();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
return response.toString();
}
public static String prettify (String json_text) {
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(json_text).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(json);
}
public static void main(String[] args) {
try {
String response = get_suggestions ();
System.out.println (prettify (response));
}
catch (Exception e) {
System.out.println (e);
}
}
}