Java captures an HTML source code

Source: Internet
Author: User
  1. PackageCom. hyq. SRC;
  2. ImportJava. Io. inputstream;
  3. ImportJava.net. url;
  4. Public ClassTest {
  5. /**
  6. * @ Param ARGs
  7. */
  8. Public Static VoidMain (string [] ARGs ){
  9. Try{
  10. Test. testnetstream ();
  11. }Catch(Exception e ){
  12. E. printstacktrace ();
  13. }
  14. }
  15. Public Static VoidTestnetstream ()ThrowsException {
  16. URL url =NewURL ("http://www.imust.cn /");
  17. Inputstream in = URL. openstream ();
  18. Byte[] B =New Byte[100000];
  19. In. Read (B );
  20. In. Close ();
  21. String S =NewString (B );
  22. System. Out. println (s );
  23. }
  24. }
package com.hyq.src;import java.io.InputStream;import java.net.URL;public class Test {/** * @param args */public static void main(String[] args) {try{Test.testNetStream();}catch(Exception e){e.printStackTrace();}} public static void testNetStream()throws Exception{URL url=new URL("http://www.imust.cn/");InputStream in=url.openStream();byte[] b=new byte[100000];in.read(b);in.close();String s=new String(b);System.out.println(s);}}

Java code
  1. PackageCom. hyq. SRC;
  2. ImportJava. Io. bufferedreader;
  3. ImportJava. Io. inputstreamreader;
  4. ImportJava.net. url;
  5. ImportJava.net. urlconnection;
  6. Public ClassTest {
  7. Public Static VoidMain (string [] ARGs)
  8. {
  9. System. Out. println (test. gethtmlsource ("http://sports.163.com/zc "));
  10. }
  11. Public StaticString gethtmlsource (string URL ){
  12. Stringbuffer STB =NewStringbuffer ();
  13. Try{
  14. Urlconnection UC =NewURL (URL). openconnection ();
  15. Bufferedreader BR =NewBufferedreader (NewInputstreamreader (UC. getinputstream (), "gb2312 "));
  16. String temp =Null;
  17. While(Temp = Br. Readline ())! =Null){
  18. STB. append (temp). append ("\ n ");
  19. }
  20. BR. Close ();
  21. }Catch(Exception e ){
  22. E. printstacktrace ();
  23. }
  24. ReturnSTB. tostring ();
  25. }
  26. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.