使用java获取网页源代码

时间:2016-02-02 13:19:46

标签: java android instagram feed

我试图用JAVA获取网页源代码,但我一直都失败了!我想从下面的链接中获取源代码。

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

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

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

1 个答案:

答案 0 :(得分:0)

我对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();
        }
    }
}