如何以編程方式下載Java網(wǎng)頁我希望能夠獲取網(wǎng)頁的html并將其保存到String,這樣我就可以對它做一些處理了。此外,我如何處理各種類型的壓縮。我將如何使用Java來做這件事呢?
3 回答
臨摹微笑
TA貢獻1982條經(jīng)驗 獲得超2個贊
URL url = new URL(urlStr);HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Cast shouldn't failHttpURLConnection.setFollowRedirects(true);
// allow both GZip and Deflate (ZLib) encodingsconn.setRequestProperty("Accept-Encoding", "gzip, deflate");
String encoding = conn.getContentEncoding();InputStream inStr = null;
// create the appropriate stream wrapper based on// the encoding typeif (encoding != null && encoding.equalsIgnoreCase("gzip")) {
inStr = new GZIPInputStream(conn.getInputStream());} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
inStr = new InflaterInputStream(conn.getInputStream(),
new Inflater(true));} else {
inStr = conn.getInputStream();}conn.setRequestProperty ( "User-agent", "my agent name");
添加回答
舉報
0/150
提交
取消
