[問題] httpclient取回來的網頁編碼問題

看板java作者 (It's up to you)時間15年前 (2010/04/04 15:22), 編輯推噓3(301)
留言4則, 3人參與, 最新討論串1/1
前輩們好 最近要抓一個網頁的資料 我用了httpclient 這個工具提供的jar 需要去下列網頁抓一些資料 我會下參數說要抓哪個國家哪個月的匯率 http://web.customs.gov.tw/currency/currency/currency.asp?qry=1&language=c 基本上是可以抓資料回來,但是中文是亂碼 我嘗試過用該網頁header的編碼來當作編碼方式也沒有效 用utf-8也沒有效 不知道要怎樣設定 才會得到正確的編碼呢 還是我哪裡寫錯了 謝謝 (雖然如果我只是要匯率數字 其他中文字是沒差 但是我還是想了解到底我哪寫錯了) 以下是程式 可否請有經驗的給予指導 謝謝 package httpdata.client; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; /** * @author user * */ public class CurrencyGetter { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://web.customs.gov.tw/currency/currency/currency.asp?qry=1&language=c"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("counter", "USD")); nvps.add(new BasicNameValuePair("year", "99")); nvps.add(new BasicNameValuePair("month", "3")); nvps.add(new BasicNameValuePair("day", "0")); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); if (entity != null) { entity.consumeContent(); } if (entity != null) { System.out.println(EntityUtils.toString(entity,HTTP.ISO_8859_1)); } httpclient.getConnectionManager().shutdown(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.164.83.43

04/04 21:15, , 1F
EntityUtils.toString(entity,"big5");
04/04 21:15, 1F

04/05 10:03, , 2F
那個網頁是big5編碼, 不是UTF-8
04/05 10:03, 2F

04/05 11:23, , 3F
原PO的寫法上 我看不出哪邊是用utf-8
04/05 11:23, 3F

04/05 18:39, , 4F
謝謝指導,原來參數直接寫"big5"就正確了
04/05 18:39, 4F
文章代碼(AID): #1Bk3v2v_ (java)