public static void main(String[] args) throws MalformedURLException { BufferedReader in = null; String result = null; try { URL url = new URL("http://www.baidu.com"); in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); StringBuffer sb = new StringBuffer(); String str = null; while ((str = in.readLine()) != null) { sb.append(str); } result = sb.toString(); System.out.println(result); } catch (Exception ex) { } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } 返回的是:
三种连接方法:
// 方法一 URL url = new URL("http://www.sina.com.cn"); URLConnection urlcon = url.openConnection(); InputStream is = urlcon.getInputStream(); // 方法二 URL url = new URL("http://www.yhfund.com.cn"); HttpURLConnection urlcon = (HttpURLConnection)url.openConnection(); InputStream is = urlcon.getInputStream(); //方法三 URL url = new URL("http://www.yhfund.com.cn"); InputStream is = url.openStream();
/** * 调用数据同步 * @param date 当前日期 格式yyyy-MM-dd * @param totalExclusion 传入“”字符串 * @return 返回结果 */ public static String queryDataSynStatus(String date, String totalExclusion) { try { System.out.println("Data Collection Date: " + date); if (date == null) return "Data Synchronized Failure!"; System.out.println("Data Synchronized Start!"); String local = "http://localhost:7070/a/b.do"; HashMapparams = new HashMap (); params.put("date", date); params.put("totalExclusion", totalExclusion); StringBuffer sb = new StringBuffer(); for (String key : params.keySet()) { sb.append(key).append("=") .append(params.get(key) != null ? params.get(key) : "") .append("&"); } sb.deleteCharAt(sb.length() - 1); // 新增接口 String param = HttpRequest.sendPost(local, sb.toString()); System.out.println(param); System.out.println("Data Synchronized End!"); } catch (Exception e) { e.printStackTrace(); } return "Data Synchronized Successfully!"; }
/** * 向指定 URL 发送POST方法的请求 * * @param url 发送请求的 URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("Host", "www.baidu.com"); conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); conn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); conn.setRequestProperty("Content-Length", "37"); conn.setRequestProperty("Cookie", "JSESSIONID=************************"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("Pragma", "no-cache"); conn.setRequestProperty("Cache-Control", "no-cache"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }