HttpCore 4 - Кодировка / Charset - PullRequest
       61

HttpCore 4 - Кодировка / Charset

0 голосов
/ 26 марта 2012

Как я могу установить кодировку результата на определенную кодировку (особенно для немецкого умляута - ä ü ö ß)

Вот что я делаю:

        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

EDIT:

это мой конструктор для некоторых элементов в коде выше, который я использую:

        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 голосов
/ 26 марта 2012

Боже мой.nevermind:)

я использовал это для кодирования входного потока в строку:

            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")));

closed :) надеюсь, это поможет всем, у кого возникла та же проблема.

...