HttpCore 4 - 编码/字符集

时间:2012-03-26 18:11:22

标签: java encoding character-encoding apache-httpcomponents

如何将结果编码设置为某个编码(特别是德语umlaute - äüöß)

这就是我的所作所为:

        BasicConnPool pool = this.cluster.getPool();
        HttpProcessor httpproc = this.cluster.getHttpproc();
        HttpRequestExecutor httpexecutor = this.cluster.getHttpExecutor();
        ConnectionReuseStrategy connStrategy = this.cluster.getConnStrategy();
        HttpContext context = new BasicHttpContext();
        URI targetUri;
        try {
            targetUri = new URI("http://www.amazon.de");
        } catch (java.net.URISyntaxException ex) {
            Control.getLogger(this.getClass()).fatal("Big Error in URI");
            return;
        }
        HttpHost targetHost = new HttpHost(targetUri.getHost(), targetUri.getPort() == -1 ? 80 : targetUri.getPort());
        Future<BasicPoolEntry> future = pool.lease(targetHost, null);
        HttpRequest request = new BasicHttpRequest("GET", "http://www.amazon.de");
        boolean reusable = false;
        try {
            BasicPoolEntry entry = future.get();
            try {
                HttpClientConnection conn = entry.getConnection();
                context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
                context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
                httpexecutor.preProcess(request, httpproc, context);
                HttpResponse response = httpexecutor.execute(request, conn, context);
                httpexecutor.postProcess(response, httpproc, context);
                HttpEntity entity = response.getEntity();
                InputStream body = entity.getContent();
                if (connStrategy.keepAlive(response, context)) {
                    reusable = true;
                }
                for (HeaderIterator it = request.headerIterator(); it.hasNext(); ) {
                    it.next();
                    it.remove();
                }
            } finally {
                pool.release(entry, reusable);
            }
        } catch (InterruptedException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (ExecutionException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (IOException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (HttpException ex) {
            Control.getLogger(this.getClass()).error("HttpException in DownloadThread.", ex);
        }

这只是一个例子。

结果包含奇怪的符号,如

  

1998-2012,Amazon.com,Inc。或者Tochtergesellschaften

编辑:

这是我正在使用的上述代码中的一些元素的构造函数:

        HttpParams params = new SyncBasicHttpParams();
        params.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false);
        params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024);
        params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 15000);

        this.httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[]{
                new RequestContent(),
                new RequestTargetHost(),
                new RequestConnControl(),
                new RequestUserAgent()
        }, null);
        this.httpexecutor = new HttpRequestExecutor();
        this.connStrategy = new DefaultConnectionReuseStrategy();
        this.pool = new BasicConnPool(params);
        this.pool.setMaxTotal(2000);

提前致谢

1 个答案:

答案 0 :(得分:0)

哦,天啊。没关系:)

我使用它来将输入流编码为字符串:

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.body));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }

            bufferedReader.close();

那么诀窍是什么?我用这个替换了第一行:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.body, Charset.forName("ISO-8859-1")));

关闭:)希望这可以帮助任何有同样问题的人。