您的位置:首页 > 其它

根据一个URL,向IE发起请求,得到这个URL里面的原始数据

2012-06-29 15:09 344 查看
public TextEntity urlToText(String url) {

TextEntity te = new TextEntity();//返回实体

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;

}

本文出自 “water” 博客,请务必保留此出处http://yxmwater.blog.51cto.com/4918893/912683
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐