【问题标题】:Getting webpage source code with java [closed]用java获取网页源代码[关闭]
【发布时间】:2016-02-02 13:19:45
【问题描述】:

我正在尝试使用 JAVA 获取网页源代码,但总是失败!我想从下面的链接获取源代码。

http://widget.websta.me/rss/n/wikirap_official

我在网上搜索并尝试了许多代码但都没有返回任何内容,此页面将我的 INSTAGRAM 用户帖子作为提要返回。

请在此链接上测试代码,如果您成功获取源代码,请与我分享代码。

【问题讨论】:

  • 请在此链接上测试代码,如果您成功获取源代码,请与我分享代码。。真的吗?编写代码,测试它,如果成功,分享代码。你为什么不雇用我?
  • @Rohit5k2 呵呵,你们认为你们的 android 开发人员必须随时为任何事情付费,而且只能为钱工作……对不起你和投反对票的人……这只是一个请求,但看起来某人需要钱
  • 哦,对了。阅读此stackoverflow.com/help/how-to-ask

标签: java android instagram feed


【解决方案1】:

我不确定Android,但这是您可以使用 Java 阅读网页源代码的方式。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class readURL  {
    public static void main(String[] args) {
        String generate_URL = "http://www.example.com";
        String inputLine;
        try {
            URL data = new URL(generate_URL);
            /**
             * Proxy code start 
             * If you are working behind firewall uncomment below lines. 
             * Set your proxy server
             */

            /* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.202", 8080)); */
            /* HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy); */

            /* Proxy code end */

            /* Open connection */
            /* comment below line in case of Proxy */
            HttpURLConnection con = (HttpURLConnection) data.openConnection(); 
            /* Read webpage coontent */
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            /* Read line by line */
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            /* close BufferedReader */
            in.close();
            /* close HttpURLConnection */
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多