根据网页地址获取页面内容
2012-04-13 17:21
309 查看
public JSONObject urlConn(String urlStr, String portStr){
String port = getPort(portStr);
urlStr = (urlStr != null)?(host + port + "/?" + urlStr):(host + port);
int responseCode;
try {
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
responseCode = conn.getResponseCode();//获取返回码
if( responseCode == HttpURLConnection.HTTP_OK ){
is = conn.getInputStream();//获取输入流
//读取数据流
bufferedReader = new BufferedReader(new InputStreamReader(is));
//建立字符串操作对象
builder = new StringBuilder();
String line = "";
while( (line = bufferedReader.readLine()) != null ){
builder.append(line);
}
bufferedReader.close();
is.close();
//json解析
jsonObject = new JSONObject(builder.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
String port = getPort(portStr);
urlStr = (urlStr != null)?(host + port + "/?" + urlStr):(host + port);
int responseCode;
try {
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
responseCode = conn.getResponseCode();//获取返回码
if( responseCode == HttpURLConnection.HTTP_OK ){
is = conn.getInputStream();//获取输入流
//读取数据流
bufferedReader = new BufferedReader(new InputStreamReader(is));
//建立字符串操作对象
builder = new StringBuilder();
String line = "";
while( (line = bufferedReader.readLine()) != null ){
builder.append(line);
}
bufferedReader.close();
is.close();
//json解析
jsonObject = new JSONObject(builder.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
相关文章推荐
- 根据网页地址获取页面内容
- 商品详情页面特殊制作,获取网页的高度,collectionView根据内容的变化其高度也随着变化
- 根据URL地址以流的形式获取页面内容
- \t\t根据URL地址以流的形式获取页面内容
- iframe的页面内容如何获取父页面的地址
- javascript 根据歌名获取播放地址和歌词内容
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- snoopy_with_thinkphp 一个根据url异步获取网页内容的thinkphp小demo
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- 爬取网页时自动获取网页编码信息,并对特殊的乱码页面(压缩过的网页内容)用gzip进行解码。
- iOS 直接改变.html后缀的链接所对应的html页面样式,并获取html的标题,图片地址,id,内容
- javascript 根据歌名获取播放地址和歌词内容
- html间页面静态传值(根据url地址拆分字符串获取参数)
- 通过网页地址获取网站HTML内容
- 利用htmpParser获取网页内容以及网页上的图片地址
- 根据URL地址获取网页信息(GET/POST)
- C# 通过url地址获取页面内容
- GO-读取网页内容(获取外网IP地址)
- 获取WebView加载HTML时网页中的内容 获取页面元素
- 根据文件夹地址获取txt文件并获取txt内容索引