Java HTTP请求失败 [英] Java HTTP Request Fail

查看:133
本文介绍了Java HTTP请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些搜索引擎上,我正在用http执行一个Java查询,这是两个类的代码:

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;
    }

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?

谢谢

编辑一个:

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

CatchQuery简单捕获txtsearch(字段)的查询.

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

编辑二:[已解决主题]

Edit Two: [Topic Solved]

推荐答案

两个问题:

  1. "http://google.com/search?q=" + query 应该是"http://google.com/search?q=" + URLEncoder.encode(query),在打开连接之前需要对查询url进行编码,以便将不受支持的字符转换为url友好字符

  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

Google不接受漫游器连接,您应该使用 GoogleJava API 以正确执行搜索

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

更新

没有用户代理标头的Google不会接受连接,因此创建连接后,您必须编辑 HttpRequest 类以设置用户代理:

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.

注意:来自 Google ToS :

自动查询

未经Google事先明确许可,Google的服务条款不允许向我们的系统发送任何形式的自动查询.发送自动查询会消耗资源,包括使用任何软件(例如WebPosition Gold)将自动查询发送给Google,以确定网站或网页在各种查询的Google搜索结果中的排名.除了排名检查之外,未经许可的其他类型的自动访问Google的行为也违反了我们的网站站长指南和服务条款.

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.

这篇关于Java HTTP请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆