Do not say anything more, take URLConnection class for example, do a get the source code of the website
public class Urldemo {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the URL:");
String urlstr = "http://" +scan.next ();
try {
URL url = new URL (urlstr); Instantiate the URLSTR string URL as a URL to locate the address s
URLConnection urlconn = Url.openconnection (); Open Web link s
BufferedReader reader = new BufferedReader (New InputStreamReader (
Urlconn.getinputstream ())); Instantiate the input stream and get the page code
string S; Loop through, until the value to read is empty
StringBuilder sb = new StringBuilder ();
while ((s = reader.readline ())!= null) {
Sb.append (s);
}
Reader.close ();
BufferedWriter bw = new BufferedWriter (New FileWriter ("d:1.html")); Write writes to file (Byte stream)
Bw.write (Sb.tostring ());
Bw.flush ();
Bw.close ();
System.out.println ("Save as success!") ");
}
catch (Exception e) {
Todo auto-generated Catch block
E.printstacktrace ();
}
}
}
We all know that the URL is the Uniform Resource Locator.
It consists of the protocol name and the resource name
The following is a Chinese character garbled processing method
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 & nbsp 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);
}
}
}