Read page content is not garbled

Source: Internet
Author: User
<span id="Label3"></p><p><p>Do you find that each time you read the page content, you have to find the encoding type of the page, this time a public method, the next time you read the page content</p></p><p><p>There will be no more garbled.</p></p><p><p></p></p><pre code_snippet_id="354910" snippet_file_name="blog_20140520_1_3059375" name="code" class="java">Package Org.httpclient;import Java.io.bufferedreader;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.url;import Org.apache.commons.lang3.arrayutils;import Com.auto.generate.log.logmanager;public class HttpClient {private String charset; Public cookie[]cookies;p ublic String useragent = "mozilla/5.0 (Windows NT 5.1) applewebkit/537.36 (khtml, like Gecko) Chr ome/31.0.1650.57 safari/537.36 "; /** * NewLine character * */public static final String line_separator = System.getproperty ("line.separator"); /** * Execute Read page code * */public string Execute (string url) {stringbuffer stringbuffer = new StringBuffer (); try {url u=new url ( url); HttpURLConnection conn = (httpurlconnection) u.openconnection (); StringBuffer cookiebuffer = new StringBuffer (); If (null! = Cookies && cookies.length > 0) {int length = 0; for T x=0;x<cookies.length;x++) {cookie cookie = cookies[x];if (length! = 0) {cookiebuffer.append (";");} Length++;cookiebuffer. append (cookie.getname ()). append ("="). append (cookie.getvalue ()); }}conn.setrequestproperty ("user-agent", useragent); conn.addrequestproperty ("Cookie", cookiebuffer.tostring ()); Conn.connect (); This.charset = Conn.getcontenttype (). replaceall (". +?charset=", ""); InputStream is = Conn.getinputstream (); BufferedReader bf = new BufferedReader (new inputstreamreader (is, charset)); String line = null, while (line = bf.readline ())! = Null) {stringbuffer.append (line). Append (line_separator); }string cookie = Conn.getheaderfield ("set-cookie"); splitcookies (cookie); Conn.disconnect (); } catch (Exception E) {logmanager.err ("http request error", e);} return stringbuffer.tostring (); }private void splitcookies (string cookie) {if (null!=cookie) {string[]cookiearray = cookie.split (";"); for (string Cookiestr:cookiearray) {string[]temparr = Cookiestr.split ("="); if (temparr.length = = 2) {cookies = Arrayutils.add ( cookies, New Cookies (temparr[0], temparr[1]));}}} /** * Gets the encoding type of the current page * */public String getcharset () {return charset;} /** * GET Web Cookie message * */public cookie[] getcookies () {return cookies;} /** * Set user-agent * */public void setuseragent (String useragent) {if (null = = UserAgent | | ". equals (useragent.trim ())) {return;} This.useragent = useragent; }public void setcookies (cookie[] cookies) {this.cookies = cookies;}}</pre><p><p><br></p></p><p><p>Cookie Class:</p></p><p><p></p></p><pre code_snippet_id="354910" snippet_file_name="blog_20140520_2_9178074" name="code" class="java"><pre code_snippet_id="354910" snippet_file_name="blog_20140520_2_9178074" name="code" class="java">Package Org.httpclient;public class Cookie {private string name; private string value;p ublic cookie (string name, string Value) {this.name = Name;this.value = value;} Public String getName () {return name;} public void SetName (String Name) {this.name = name;} Public String getValue () {return value;} public void SetValue (String Value) {this.value = value;}}</pre></pre><br><br><p><p></p></p><br><p><p></p></p></span>

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.