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