public TextEntity urlToText(String url) {
InputStream is=null;
BufferedReader br=null;
InputStreamReader il=null;
HttpURLConnection conn=null;
StringBuilder sb = new StringBuilder();
if(url != null && !url.equals("")) {
//发起URL请求
String logs = "----------------解析web页面-----------------"+"\r\n";
logs += "url: "+url+"\r\n";
try {
java.net.URL urll = new URL(url);
conn= (HttpURLConnection) urll.openConnection();
conn.setConnectTimeout(50000); //设置超时时间
is=conn.getInputStream();
il=new InputStreamReader(is,"utf-8");
br = new BufferedReader(il);
String temp;
br.readLine();
while ((temp = br.readLine()) != null) {
sb.append(temp).append("\n");
}
}catch(Exception e) {
e.printStackTrace();logs+="Exception "+e.toString()+"\r\n";
te.setErrorCode("fail");
te.setErrorText("请求发送失败");
}
finally {
try{
if(br!=null){
br.close();
}
}catch(Exception e) {
e.printStackTrace();
}
try {
if(il!=null){
il.close();
}
}catch(Exception e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
}catch(Exception e) {
e.printStackTrace();
}
try{
if(conn!=null){
conn.disconnect();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}else {
te.setErrorCode("fail");
te.setErrorText("URL为空");
}
return te;
}
转载于:https://blog.51cto.com/yxmwater/912683