根据url获取网页内容

PrintWriter out = null;out = response.getWriter();try{URL getUrl = new URL(“http://www.kuaidi100.com/applyurl?key=”+KEY+”&com=”+com+”&nu=”+nu);//System.out.println(“getUrl:”+getUrl);// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,,// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnectionHttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到// 服务器connection.connect();// 取得输入流,并使用Reader读取BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),”utf-8”));//设置编码,否则中文乱码String lines;while ((lines = reader.readLine()) != null){//lines = new String(lines.getBytes(), “utf-8”);out.print(lines);//输出网页内容}reader.close();// 断开连接connection.disconnect();}catch(Exception e){e.printStackTrace();}finally {out.close();}你看报表时,梅里雪山的金丝猴刚好爬上树尖。

根据url获取网页内容

相关文章:

你感兴趣的文章:

标签云: