简体   繁体   English

HTTP URL连接响应

[英]HTTP URL connection response

I am trying to hit the URL and get the response from my Java code. 我正在尝试访问URL并从Java代码获得响应。

I am using URLConnection to get this response. 我正在使用URLConnection获得此响应。 And writing this response in html file. 并将此响应写入html文件。

When opening this html in browser after executing the java class, I am getting only google home page and not with the results. 在执行Java类后在浏览器中打开此html时,我只得到google主页,而不得到结果。

Whats wrong with my code, my code here, 我的代码有什么问题,我的代码在这里,

    FileWriter fWriter = null;
    BufferedWriter writer = null;

    URL url = new URL("https://www.google.co.in/?gfe_rd=cr&ei=aS-BVpPGDOiK8Qea4aKIAw&gws_rd=ssl#q=google+post+request+from+java");
    byte[] encodedBytes = Base64.encodeBase64("root:pass".getBytes());
    String encoding = new String(encodedBytes);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", "Mozilla/5.0");
    connection.setRequestProperty("Accept-Charset", "UTF-8");
    connection.setDoInput(true);        
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    connection.connect();

    InputStream content = (InputStream) connection.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(content));
    String line;

    try {
        fWriter = new FileWriter(new File("f:\\fileName.html"));
        writer = new BufferedWriter(fWriter);
        while ((line = in.readLine()) != null) {
            String s = line.toString();
            writer.write(s);
            }
    writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Same code works couple of days back, but not now. 相同的代码几天前就可以使用,但现在不行了。

This method of searching is not advised is supposed to fail, you must use google search APIs for this kind of work. 不建议这种搜索方法失败,您必须使用google搜索API进行此类工作。

Note: Google uses some redirection and uses token, so even if you will find a clever way to handle it, it is ought to fail in long run. 注意: Google使用一些重定向并使用令牌,因此即使您找到了一种巧妙的处理方式,它也应该长期失败。

Edit: 编辑:

This is a sample of how using Google search APIs you can get your work done in reliable way; 这是使用Google搜索API如何以可靠方式完成工作的示例; please do refer to the source for more information. 请确实参考以获取更多信息。

public static void main(String[] args) throws Exception {
    String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
    String search = "stackoverflow";
    String charset = "UTF-8";

    URL url = new URL(google + URLEncoder.encode(search, charset));
    Reader reader = new InputStreamReader(url.openStream(), charset);
    GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

    // Show title and URL of 1st result.
    System.out.println(results.getResponseData().getResults().get(0).getTitle());
    System.out.println(results.getResponseData().getResults().get(0).getUrl());
}

The reason is that this url does not return search results it self. 原因是此网址不会自行返回搜索结果。 You have to understand google's working process to understand it. 您必须了解Google的工作流程才能了解它。 Open this url in your browser and view its source. 在浏览器中打开该URL并查看其源。 You will only see lots of javascript there. 您在那只会看到很多JavaScript。

Actually, in a short summary, google uses Ajax requests to process search queries. 实际上,在简短的摘要中,google使用Ajax请求来处理搜索查询。

To perform required task you either have to use a headless browser (the hard way) which can execute javascript/ajax OR better use google search api as directed by anand . 要执行所需的任务,您要么必须使用无头浏览器(困难的方式)即可执行javascript / ajax,要么最好按照anand的指示使用google search api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM