根据URL路径获取路径内容 Java方法

tech2022-10-24  89

/** * 根据string字符串转换为URL进行访问,返回该路径内容 * @param path * @return */ public String getUrlContent(String path){ String rtn = “”; int c; try{ java.net.URL l_url = new java.net.URL(path); java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection(); l_connection.setRequestProperty(“User-agent”,“Mozilla/4.0”); l_connection.connect(); InputStream l_urlStream = l_connection.getInputStream(); while (( ( c= l_urlStream.read() )!=-1)){ int all=l_urlStream.available(); byte[] b =new byte[all]; l_urlStream.read(b); rtn+= new String(b, “UTF-8”); } //Thread.sleep(2000); l_urlStream.close(); }catch(Exception e){ e.printStackTrace(); } return rtn; }

最新回复(0)