传入一个url,返回
源代码; public static String getHTML(String url){// 获取指定URL的网页,返回网页内容的
字符串,然后将此字符串存到文件即可 try { URL newUrl = new URL(url)URLConnection connect = newUrl.openConnection()connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatibleMSIE 5.0Windows NTDigExt)")DataInputStream dis = new DataInputStream(connect.getInputStream())BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8"))String html = ""String readLine = nullwhile((readLine = in.readLine()) != null) { html = html + readLine} in.close()return html}catch (MalformedURLException me){ System.out.println("MalformedURLException" + me)}catch (IOException ioe){ System.out.println("ioeException" + ioe)} return null}var xml = new ActiveXObject("Msxml2.XMLHTTP")
xml.open("get", "http://www.baidu.com",false)
xml.send(null)
if(xml.readyState != 4 || xml.status != 200){
alert("出错了")
}else{
// xml.responseText就是你要的网址页面内容
alert(xml.responseText)
}
评论列表(0条)