Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HTTP请求失败_Java_Url_Httprequest_Urlconnection - Fatal编程技术网

Java HTTP请求失败

Java HTTP请求失败,java,url,httprequest,urlconnection,Java,Url,Httprequest,Urlconnection,我在一些搜索引擎上使用http执行一个java查询,下面是两个类的代码: public EventSearch(){ btsearch.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==btsearch){ try { HttpRequest http = new Htt

我在一些搜索引擎上使用http执行一个java查询,下面是两个类的代码:

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 String CatchQuery(){
            query=txtsearch.getText();
            return query;
        }
CatchQuery简单捕获txtsearch(字段)的查询

编辑两个:[主题已解决]

两个问题:

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

  • Google不接受bot连接,您应该使用来正确执行搜索

  • 更新

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

    // 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");
    
    它对我有效,测试一下,告诉我它是否对你也有效

    注:发件人:

    自动查询 谷歌的服务条款不允许在没有谷歌事先明确许可的情况下向我们的系统发送任何形式的自动查询。发送自动查询会消耗资源,包括使用任何软件(如WebPosition Gold)向谷歌发送自动查询,以确定网站或网页在谷歌各种查询搜索结果中的排名。除了排名检查,其他类型的未经许可自动访问谷歌也违反了我们的网站管理员指南和服务条款


    CatchQuery()
    方法做什么?@反斜杠谢谢你的修改,看看我做的编辑。捕获字符串非常简单。你没有得到消息,因为你已经隐藏了它们-在捕获中,用
    e.printStackTrace()
    打印堆栈跟踪,或者,至少,将
    e.getMessage()
    写入System.out。@VictorOliveira Ray意味着你拥有的每个捕获块都在摆脱抛出的异常,将
    e.printStackTrace()
    添加到每个catch块中,您将看到抛出了哪个特定的异常,并且where@VictorOliveira尝试更改
    url=新url(“http://google.com/search?q=“+查询)
    url=新url(“http://google.com/search?q=“+URLEncoder.encode(查询))有效吗?非常感谢你的@反斜杠现在我在问自己。。你使用了一些特殊的信息,比如mozila、linux等等,我需要一个通用的应用程序,所以使用google API不是更好吗?@VictorOliveira特定的信息只是标题,你可以将它们设置为任何你想要的,例如,对我来说(我使用的是google chrome),它是
    Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,比如Gecko)Chrome/28.0.1500.52 Safari/537.36
    。是的,最好使用谷歌API,我的建议只是如何让你的代码正常工作并下载搜索结果而不出现403错误。我会尽快看一看,泰!
    // 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");