读取网页内容不在出现乱码

2020-12-13 06:04

阅读:481

标签:blog   class   c   code   java   http   

有没有发现每次读取网页内容时,都要去找网页的编码类型,这次研究出来一个公共的方法,下次读取网页内容时

再不会出现乱码了.

package 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 ;
	
	public String userAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36" ; 
	/**
	 * 换行字符
	 * */
	public static final String LINE_SEPARATOR = System.getProperty("line.separator") ; 
	
	/**
	 * 执行读取网页代码
	 * */
	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(int x=0;x


cookie类:

package org.httpclient;

public class Cookie {
	
	private String name ; 
	
	private String value ;
	
	public 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;
	}
	
}


读取网页内容不在出现乱码,搜素材,soscw.com

读取网页内容不在出现乱码

标签:blog   class   c   code   java   http   

原文地址:http://blog.csdn.net/hfmbook/article/details/26371127


评论


亲,登录后才可以留言!