Read page content is not garbled
Last Update:2014-05-21
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>