Java HTTP Request Fail

2019-09-11 02:51发布

问题:

I'm doing one java query with http on some search engines and here is the code of two classes:

public EventSearch(){

    btsearch.addActionListener(this);

}

    public void actionPerformed(ActionEvent e){

        if(e.getSource()==btsearch){


            try {
                HttpRequest http = new HttpRequest(CatchQuery());
            } catch (IOException e1) {
                JOptionPane.showMessageDialog(null, "HTTP request failure.");
            }   
            this.dispose();
        }

    }

    public String CatchQuery(){
        query=txtsearch.getText();
        return query;
    }

and

public class HttpRequest extends EventSearch 
{
    String query;
    URL url;

public HttpRequest(String query) throws IOException{
    // Fixed search URL; drop openConnection() at the end

    try {
        url = new URL("http://google.com/search?q="+query);
        System.out.println(CatchQuery());
    } catch (MalformedURLException e) {
        JOptionPane.showMessageDialog(null, "Unable to search the requested URL");
    }


    // Setup connection properties (this doesn't open the connection)
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Accept-Charset", "UTF-8");

    // Setup a reader
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    // Read line by line
    String line = null;
    while ((line = reader.readLine()) != null) {
         System.out.println (line);
    }

    // Close connection
    reader.close();
}

The thing is - There are no errors regarding the code but the request is stucked. I don't receive any sort of message on my console our debug. I'm thinking of any sort of memory error since I'm working with strings but anyone has any idea of whats going wrong on?

thank you

Edit One:

public String CatchQuery(){
            query=txtsearch.getText();
            return query;
        }

CatchQuery Simple catch the query of the txtsearch (field).

Edit Two: [Topic Solved]

回答1:

Two problems:

  1. "http://google.com/search?q="+query should be "http://google.com/search?q="+URLEncoder.encode(query), query url needs to be encoded before opening a connection, so that unsupported characters are converted to url-friendly characters

  2. Google does not accept bot connections, you should use the Google Java API to perform searches properly

UPDATE

Google does not accept connections without the User Agent header, so you have to edit the HttpRequest class to set the user agent after creating the connection:

// Setup connection properties (this doesn't open the connection)
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
connection.setRequestProperty("Accept-Charset", "UTF-8");

It works for me, test it and tell me if it works for you too.

Note: from Google ToS:

Automated queries

Google's Terms of Service do not allow the sending of automated queries of any sort to our system without express permission in advance from Google. Sending automated queries consumes resources and includes using any software (such as WebPosition Gold) to send automated queries to Google to determine how a website or webpage ranks in Google search results for various queries. In addition to rank checking, other types of automated access to Google without permission are also a violation of our Webmaster Guidelines and Terms of Service.