我正在一些搜索引擎上使用 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 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();
}
问题是 - 代码没有错误,但请求被卡住。我的调试控制台上没有收到任何类型的消息。由于我正在使用字符串,因此我正在考虑任何类型的内存错误,但有人知道出了什么问题吗?
谢谢
编辑一个:
public String CatchQuery(){
query=txtsearch.getText();
return query;
}
CatchQuery 简单捕获txtsearch(字段)的查询。
编辑二:[主题已解决]
最佳答案
两个问题:
“http://google.com/search?q="+query
”应为"http://google.com/search?q="+URLEncoder .encode(query)
,在打开连接之前需要对查询url进行编码,以便将不支持的字符转换为url友好的字符Google 不接受机器人连接,您应该使用 Google Java API正确执行搜索
更新
Google 不接受没有用户代理 header 的连接,因此您必须在创建连接后编辑 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");
它对我有用,测试一下并告诉我它是否也适合你。
注:来自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.
关于Java HTTP 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17259959/