HttpClient请求的结果中中文显示乱码,可用UTF-8

来源:互联网 发布:mac 垃圾清理软件 编辑:程序博客网 时间:2024/06/06 02:56


import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;class HTTPGetSample {public static void main(String[] args) throws ClientProtocolException, IOException {String aString = getHTTPReturn("http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=zzf&st=desc&sd=2015-04-07&ed=2016-04-07&qdii=&tabSubtype=,,,,,&pi=1&pn=10000&dx=1&v=0.8509733614046127");ArrayList<String> fundNum = new ArrayList<String>();// 获取代码和名称fundNum.addAll(fundName("\"([0-9]+),(.+?),", aString));}public static ArrayList<String> fundName(String regex, String resource) {ArrayList<String> arrayList = new ArrayList<String>();Matcher m = Pattern.compile(regex).matcher(resource);while (m.find()) {arrayList.add(m.group());}return arrayList;}public static String getHTTPReturn(String url) throws ClientProtocolException, IOException {HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet(url);HttpResponse response = client.execute(request);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));String tempStr;if ((tempStr = rd.readLine()) != null)return tempStr;elsereturn "";}}



0 0
原创粉丝点击