Java reads the webpage source code, java reads the source code
Package com. sphere. service; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. httpURLConnection; import java.net. URL; public class QueryService {/*** initiate an http get request to obtain the webpage source code * @ param requestUrl * @ return * @ throws IOException */private static String httpRequest (String requestUrl) throws IOException {StringBuffer buffer = Null; HttpURLConnection httpUrlConn = null; InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try {// establish connection URL url = new URL (requestUrl ); httpUrlConn = (HttpURLConnection) url. openConnection (); httpUrlConn. setDoInput (true); httpUrlConn. setRequestMethod ("GET"); // gets the input stream inputStream = httpUrlConn. getInputStream (); // http://www.bjsubway. Com/support/swzl/the webpage is gb2312 encoded inputStreamReader = new InputStreamReader (inputStream, "gb2312"); bufferedReader = new BufferedReader (inputStreamReader ); // read the returned result buffer = new StringBuffer (); String str = null; while (str = bufferedReader. readLine ())! = Null) {buffer. append (str) ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {// release the resource and close the stream if (bufferedReader! = Null) {bufferedReader. close ();} if (inputStreamReader! = Null) {inputStreamReader. close ();} if (inputStream! = Null) {inputStream. close () ;}// release the resource and close the http connection if (httpUrlConn! = Null) {httpUrlConn. disconnect () ;}} return buffer. toString ();} public static void main (String [] args) throws IOException {String Url = "http://www.bjsubway.com/support/swzl/"; String htmlString = httpRequest (Url); System. out. println (htmlString );}}