Java HTTP请求失败

编程入门 行业动态 更新时间:2024-10-09 10:23:22
本文介绍了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("google/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]

推荐答案

两个问题:

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

  • "google/search?q="+query should be "google/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请求失败

    本文发布于:2023-11-26 19:18:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634860.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Java   HTTP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!