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

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();

你可能感兴趣的:(Java)