Get page encoding using Java.net.URL

Source: Internet
Author: User

In the same class

You need to import the following packages:

Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.net.URLConnection;

1 @Test2      Public voidE ()throwsmalformedurlexception, ioexception{3 System.out.println (Testgetcharset ());4     }5      PublicString Testgetcharset ()throwsmalformedurlexception, ioexception{6         /**7 * Get an encoded form of a webpage8          *9          * @paramURLTen          */ OneString host= "Proxy3.bj.petrochina"; AString port= "8080";  - SetProxy (host,port); -String url= "Http://www.songtaste.com"; theURLConnection UC =Newurl (url). OpenConnection (); -Uc.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows XP; Digext) "); -  -InputStream is =Uc.getinputstream (); +BufferedReader br =NewBufferedReader (NewInputStreamReader (IS)); -String str =NewString (); +String temp =NULL; A             inti = 0; at              while(temp = Br.readline ())! =NULL) -             { -str + =temp; -                 if(Temp.length () >6 && temp.substring (0, 6). Equals ("<body>")) -                      Break; -                 if(++i > 50) in                      Break; -             } to br.close (); +              -             //compare equals CharSet, then find the value of CharSet the              for(i=0; I<str.length ()-7; i++)  *             { $                 if(Str.substring (i, i+7). Equalsignorecase ("CharSet"))Panax Notoginseng                 { -i = i+7; the                      Break; +                 } A             } the             intBegin = 0; +              while(true)  -             { $                 if(I >=str.length ()) $                     return NewString (); -                 Charc =Str.charat (i); -                 if(c!= ' && c!= ' = ') the                 { -                     if(Begin = = 0)WuyiBegin =i; the                     if(c = = ' "')) -                         returnstr.substring (begin, I); Wu                 } -i++; About             } $         } -      -      Public Static voidSetProxy (string host, string port) { -System.setproperty ("Proxyset", "true");  ASystem.setproperty ("ProxyHost", host);  +System.setproperty ("ProxyPort", Port);  the}

Get page encoding using Java.net.URL

Related Article

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.