【发布时间】:2011-12-29 20:00:07
【问题描述】:
我要获取https://www2.cslb.ca.gov/OnlineServices/CheckLicenseII/LicenseDetail.aspx?LicNum=872423的html源码
为此,我正在使用这种方法,但没有获得 html 源代码。
public static String getHTML(URL url) {
HttpURLConnection conn; // The actual connection to the web page
BufferedReader rd; // Used to read results from the web page
String line; // An individual line of the web page HTML
String result = ""; // A long string containing all the HTML
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
【问题讨论】:
-
rd.readLine()行第一次为空。 -
你能给我们提供更多的背景信息吗? “没有得到 html 源代码”是什么意思?
-
@JavierIEH 方法返回空字符串
-
你试过
curl(命令行工具)来获取html吗?某些网站会检查请求是否来自网络浏览器。 -
@gigadot 我认为服务器可以通过查看
User-Agenthttp 标头来判断请求是否来自浏览器。还有其他方法可以检查吗?
标签: java html url httpurlconnection