Here's the Java code to generate a random pronounceable word between 3 and 10 characters, then perform a Bing search with it and load the first result HTML URL content to a String value.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
public class BingSearch {
private static final String BING_SEARCH_URL = "https://www.bing.com/search?q=";
private static final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
String randomWord = getRandomPronounceableWord(3, 10);
System.out.println("Random word: " + randomWord);
String searchUrl = BING_SEARCH_URL + randomWord;
String htmlContent = getHtmlContentFromUrl(searchUrl);
System.out.println("First search result HTML content: " + htmlContent);
}
private static String getRandomPronounceableWord(int minLength, int maxLength) {
String[] consonants = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "qu", "r", "s", "t", "v", "w", "x", "y", "z"};
String[] vowels = {"a", "e", "i", "o", "u"};
Random rnd = new Random();
int length = minLength + rnd.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
if (i % 2 == 0) {
sb.append(consonants[rnd.nextInt(consonants.length)]);
} else {
sb.append(vowels[rnd.nextInt(vowels.length)]);
}
}
return sb.toString();
}
private static String getHtmlContentFromUrl(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new Exception("Error fetching HTML content from URL: HTTP " + responseCode);
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder htmlContent = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
htmlContent.append(inputLine);
}
in.close();
return htmlContent.toString();
}
}
The getRandomPronounceableWord method generates a random pronounceable word by alternating consonants and vowels from pre-defined arrays. The length of the word is randomly chosen between the given minimum and maximum lengths.
The getHtmlContentFromUrl method takes a URL as input and performs an HTTP GET request to fetch the content of the webpage. The method returns a String containing the HTML content of the webpage.
In the main method, a random pronounceable word is generated and used to build a search URL for Bing. The HTML content of the first search result is fetched and printed to the console.