使用 java 获取网页源代码
Getting webpage source code with java
我正在尝试使用 JAVA 获取网页源代码,但我总是失败!我想从下面的 link 获取源代码。
http://widget.websta.me/rss/n/wikirap_official
我在网上搜索并尝试了很多代码,但都return什么也没有,这个页面 return 我的 INSTAGRAM 用户帖子作为提要。
请在此link上测试代码,如果您成功获取源代码,请与我分享代码。
我不确定 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();
}
}
}
我正在尝试使用 JAVA 获取网页源代码,但我总是失败!我想从下面的 link 获取源代码。
http://widget.websta.me/rss/n/wikirap_official
我在网上搜索并尝试了很多代码,但都return什么也没有,这个页面 return 我的 INSTAGRAM 用户帖子作为提要。
请在此link上测试代码,如果您成功获取源代码,请与我分享代码。
我不确定 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();
}
}
}