1

i have a problem when trying to do a google search from Java.

 URL url = new URL("http://www.google.com/cse?searchtype=image&as_filetype=jpg&start=0&num=5&q=booba&client=google-csbe&cx=xxxxxxxxxxxxxxxxxxx");
        URLConnection connection = url.openConnection();

        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((line = reader.readLine()) != null) {
            builder.append(line);
            System.out.println(line);
        }

the result is like this:

302 Moved
The document has moved

could somebody help me please?

thanks

Greg Artisi
  • 172
  • 2
  • 12

3 Answers3

2

This is how Google is choosing to redirect you to a different URL. The 'raw' HTTP response will look something like this:

HTTP/1.1 302 Found
Date: Sat, 01 May 2017 20:51:40 GMT
Content-Type: text/html; charset=utf-8
Location: https://cse.google.com/cse?searchtype=image&as_filetype=jpg&start=0&num=5&q=booba&client=google-csbe&cx=xxxxxxx-xxxxxxx
Connection: Keep-Alive
Content-Length: 325

<!DOCTYPE html>
<html>
...
</html>

The key here is that Location header which indicates the new URL. Java's URLConnection should follow the redirect automatically. However, this is not the case when the redirect would upgrade the connection from HTTP to HTTPS (as it does here).

You should either initiate the connection securely using HTTPS, or parse out the Location header from each 302 response and create a new connection to that URL.

Community
  • 1
  • 1
MTCoster
  • 5,868
  • 3
  • 28
  • 49
  • i tried to initiate the connection with https. now i have an IOException java.io.IOException: Server returned HTTP response code: 400 for URL: https://cse.google.com/ – Greg Artisi May 01 '17 at 20:10
  • Can you post a gist or pastebin of the entire raw response (like I quoted in the answer)? – MTCoster May 01 '17 at 20:11
  • thank you, i have no more response, now i only have an IO exception :sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) the exception is thrown by this line : connection.getInputStream(), – Greg Artisi May 01 '17 at 20:22
  • if i do it directly on google,i have this result: Error 400 (Bad Request)!!1 it seems that my request is not correct – Greg Artisi May 01 '17 at 20:25
  • Okay, come back if it still doesn't work once your request is correct – MTCoster May 01 '17 at 20:46
  • hello, my request is now correct according to google API but now i have a response 400 with absolutely nothing written,neither in my console and in google directly, blank page:https://www.google.com/cse?searchtype=image&start=0&num=5&q=monkey&as_filetype=png&client=google-csbe&output=xml_no_dtd&cx=xxxxxxxx-xxxxxxxx – Greg Artisi May 03 '17 at 07:11
  • i changed my request, now, i have a 403 error: { "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." } } – Greg Artisi May 03 '17 at 07:50
  • Your new 403 error has the reason right there in the response: `Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup` – MTCoster May 03 '17 at 12:23
0

i changed my request, now, i have a 403 error:

{ "error": { "errors": [ {
 "domain": "usageLimits", 
 "reason": "dailyLimitExceededUnreg", 
 "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use  requires signup.",
 "extendedHelp": "code.google.com/apis/console"; } ], 
 "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded

it could be a problem caused by the google server Key that i should get in the console for developper of google, BUT it is impossible to access the console, it gives me a blank page when i try to go there:

https://console.developers.google.com/project

do you know why?

Greg Artisi
  • 172
  • 2
  • 12
  • The link you provided works fine for me, albeit on mobile. Try the usual suspects: clear cache and cookies, restart your browser, etc – MTCoster May 03 '17 at 12:24
  • thank you, the problem was the chrome navigator, i am on XP and it is not updated any more – Greg Artisi May 03 '17 at 12:31
0

ok, i found the answer, to use custom search, you need to have a cx value And a key value wich you can get https://console.developers.google.com/ For your request to work, you also need to put the start parameter at minimum 1, because 0 gives you an error 400 invalid value

this post helped me a lot:

Google image search says api no longer available

and this is my request:

https://www.googleapis.com/customsearch/v1?key=xxxxxxx&searchtype=image&start=1&num=5&q=monkey&as_filetype=png&client=google-csbe&output=xml_no_dtd&cx=xxxxxx
Community
  • 1
  • 1
Greg Artisi
  • 172
  • 2
  • 12