Zzzzzzt丶 2016-11-21 13:35 采纳率: 0%
浏览 3682

java爬网页中,输出的网页中文全是乱码

 package com.lib;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class HttpGetUtils {
    public String get(String url){
        String result = "";
        try{
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(httpget);
            try{
                if (response != null && response.getStatusLine().getStatusCode()
                        == HttpStatus.SC_OK ){
                    System.out.println(response.getStatusLine());
                    HttpEntity entity = response.getEntity();
                    System.out.println(entity.getContentEncoding());
                    result = readResponse(entity, "UTF-8");
                }
            }
            finally{
                httpclient.close();
                response.close();
            }

        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return result;
    }

    public String readResponse(HttpEntity entity, String charset){
        StringBuffer res = new StringBuffer();
        BufferedReader reader = null;
        try{
            if (entity == null){
                return null;
            }
            else{
                reader = new BufferedReader(new InputStreamReader(entity.getContent(),charset));
                String line;
                while ( (line = reader.readLine()) != null){
                    line = line + "\n";
                    res.append(line);
                }
            }
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally{
            try{
                if (reader != null){
                    reader.close();
                }

            }
            catch(Exception e){
                e.toString();
            }
        }
        return res.toString();
    }


}

  • 写回答

1条回答

  • threenewbee 2016-11-21 13:57
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法