mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 02:53:57 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
42
Java/JavaDemo/codedemo/jetty/HttpGet.java
Normal file
42
Java/JavaDemo/codedemo/jetty/HttpGet.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package cn.aofeng.demo.jetty;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
/**
|
||||
* 抓取页面内容。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class HttpGet {
|
||||
|
||||
public String getSomeThing(String urlStr) throws IOException {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
|
||||
urlConn.setConnectTimeout(3000);
|
||||
urlConn.setRequestMethod("GET");
|
||||
urlConn.connect();
|
||||
|
||||
InputStream ins = null;
|
||||
try {
|
||||
if (200 == urlConn.getResponseCode()) {
|
||||
ins = urlConn.getInputStream();
|
||||
ByteArrayOutputStream outs = new ByteArrayOutputStream(1024);
|
||||
IOUtils.copy(ins, outs);
|
||||
return outs.toString("UTF-8");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(ins);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user