也不多说什么了,就拿urlconnection类举个例子吧,做一个获取网站源码的代码
public class urldemo {
public static void main(string[] args) {
scanner scan = new scanner(system.in);
system.out.println("请输入网址:");
string urlstr ="http://"+scan.next();
try {
url url = new url(urlstr); // 将urlstr字符串网址实例化为url定位地址surlconnection urlconn = url.openconnection(); //打开网站链接s
bufferedreader reader = new bufferedreader(new inputstreamreader(
urlconn.getinputstream())); //实例化输入流,并获取网页代码
string s; //依次循环,至到读的值为空
stringbuilder sb = new stringbuilder();
while ((s = reader.readline()) != null) {
sb.append(s);
}
reader.close();
bufferedwriter bw = new bufferedwriter(new filewriter("d:1.html")); //write写入文件(字节流)
bw.write(sb.tostring());
bw.flush();
bw.close();
system.out.println("另存为成功!");
}catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}}
}
大家都知道url就是统一资源定位器
它由协议名和资源名组成
下面一个汉字中文乱码处理方法
package mynet;
import java.io.ioexception;
import java.io.inputstream;
import java.net.malformedurlexception;
import java.net.url;
import java.util.date;import sun.net.www.protocol.http.httpurlconnection;
public class urldemo {
public static void main(string[] args) {system.out.println( "starting... ");
int c;httpurlconnection urlcon = null;
try {
url url = new url( "http://www.111cn.net ");
try {
urlcon = (httpurlconnection)url.openconnection();
} catch (ioexception e) {}
system.out.println( "the date is : " + new date(urlcon.getdate()));
system.out.println( "content_type : " + urlcon.getcontenttype());
try {
inputstream in = urlcon.getinputstream();
int all= in.available();
byte[] b= new byte[all];
// while (((c = in.read()) != -1)) {
// system.out.print((char) c);
// }
in.read(b);
string webpage = new string(b, "utf-8 ");in.read(b);
string webpage = new string(b, "utf-8 ");
in.close();
system.out.println(webpage);
} catch (ioexception e) {
system.out.println( " " + e);
}} catch (malformedurlexception e) {
system.out.println( " " + e);
}}
}