您的位置:首页 > 理论基础 > 计算机网络

java发送http请求并获取response信息

2013-11-14 14:08 344 查看
private static String getHttpResponse(String employeeId) {

String url_str = URL_PREFIX + employeeId;

URL url = null;

HttpURLConnection connection = null;

String body = null;

try {

url = new URL(url_str);

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection

.setRequestProperty(

"User-Agent",

"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");

connection.connect();

InputStream in = (InputStream) connection.getInputStream();

SoftwareAuditTool.encoding = connection.getContentEncoding();

encoding = encoding == null ? "UTF-8" : encoding;

body = org.apache.commons.io.IOUtils.toString(in, encoding);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return body;

}

获取返回html的页面元素:

Document document = Jsoup.parse(htmlContent);

Elements form = document.select("form");

Elements table = form.select("table");

Elements trs = table.get(table.size() - 1).select("tr");

int totalTrs = trs.size();

Elements ths = trs.get(0).select("th");

int totalThs = ths.size();

String str = ths.get(k).select("span").html().toString();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: