Я пытаюсь сделать запрос в Google, используя Java и HttpURLConnection.
Ниже приведен мой код, где я пытаюсь сделать запрос о предоставлении домена.
public static void googleSearch(String domain) throws Exception {
String response = null;
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0;en-EN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
Map<String, String> map = new HashMap<String, String>();
map.put("User-Agent", userAgent);
map.put("Host", "www.google.com");
map.put("Cache-Control", "no-cache");
URL url = new URL("http://www.google.com/search?num=100&start=0&hl=en&meta=&q=%40%22"+domain+"%22");
response = response + Utility.GETRequest(url, map);}
Код GETRequest:
public static String GETRequest(URL url, Map<String, String> map){
String readLine = null;
String responseGET = null;
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
if(map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
con.setRequestProperty(entry.getKey(), entry.getValue());
}
}
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in .readLine()) != null) {
response.append(readLine);
response.append('\r');
}
in.close();
responseGET = response.toString();
} else {
System.out.println("responseCode = "+responseCode);
}
}catch(Exception e) {
System.out.println("Error");
}finally {
if (con != null) {
con.disconnect();
}
}
return responseGET;
}
Проблема в том, что я получаю каждый раз StatusCode = 429, пробуя с "https://www.google.com/se.....", это то же самое.
Как я могу это исправить? или я должен использовать клиентский API Google?
Я буду рад, если кто-то может решить это.