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();
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();
相关文章推荐
- Java在发送HTTP请求时,遇到身份验证无法获取到response内容的解决方法
- jmeter 发送http请求,并把获取到的请求的订单信息保存到文件中
- [疯狂Java]UDP:接收发送数据报、获取接收到的数据报的相关信息
- java发送http请求并获取状态码的简单实例
- java发送http请求并获取状态码
- java 发送POST、GET请求时,获取请求的头信息Set-Cookie,请求携带Cookie
- Java发送Http请求并获取状态码
- java发送http请求并获取状态码
- 使用JAVA发送HTTP请求(Http Request),返回HTTP响应(Http Response)内容,代码程序例子及原理说明
- java内部发送http请求并取得返回结果,修改response的cookie
- Java发送http请求并为http设置头信息
- java发送http请求获取响应结果【工具包系列】
- Java程序使用httpClient发送Url链接,并获取返回的数据信息
- java发送http请求并获取状态码
- java 发送 json、xml格式的 http请求,并读取响应response内容实例
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- java发送http请求并获取状态码
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- java发送http请求并获取状态码
- java获取计算机相关的信息