//工具类
public class HttpUtils {
public static String readURL(String path){
try {
URL url = new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setConnectTimeout(1000);
openConnection.setReadTimeout(1000);
openConnection.connect();
int responseCode = openConnection.getResponseCode();
if(200==responseCode){
InputStream inputStream = openConnection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] by = new byte[1024];
int len = -1;
while((len = inputStream.read(by))!=-1){
bos.write(by, 0, len);
}
return bos.toString("utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String readUrl(String url) throws Exception {
// 创建get对象
HttpGet get = new HttpGet(url);
// 创建clinent
HttpClient client = new DefaultHttpClient();
// 发送请求
HttpResponse response = client.execute(get);
// 获取成功
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
String content = EntityUtils
.toString(response.getEntity(), "utf-8");
return content;
}
return "";
}