第一种:URL
package InternetTest; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class a44 { public static void main(String[] args) throws Exception { URL url = new URL(";); HttpURLConnection conn = (HttpURLConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1024); InputStream inStream = conn.getInputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inS(buffer)) != -1) { ou(buffer, 0, len); } inS(); byte[] data =ou(); String htmlSource = new String(data); Sy(htmlSource); } }
第二种:HttpClient
package InternetTest; import org.a; import org.a; import org.a; import org.a; import org.a; import org.a; import org.a; import org.a; public class a45 { public static void main(String[] args) throws Exception{ String url1 = ";; CloseableHttpClient closeableHttpClient = H(); CloseableHttpResponse closeableHttpResponse = null; HttpGet request = new HttpGet(url1); closeableHttpResponse = clo(request); i().getStatusCode() == H) { HttpEntity httpEntity = clo(); String html = En(httpEntity, "utf-8"); Sy(html); } else { Sy(En(clo(), "utf-8")); } H(closeableHttpResponse); H(closeableHttpClient); } }