如何从org.apache.http.client.methods.HttpGet支持安全的http连接

时间:2011-12-03 19:18:56

标签: java http

我正在使用org.apache.http.client.methods.HttpGet类下载图片,但我发现它不支持https图片。这是代码(实际上来自一位前同事离开):

public HttpResponse getHttpResponse(String url, Map<String, String> headers, String username, String password) {
    try {
        HttpGet httpGet = new HttpGet(url);
        if (headers != null && !headers.isEmpty()) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                httpGet.addHeader(header.getKey(), header.getValue());
            }
        }
        if (username != null && password != null) {
            addBasicAuthentication(httpGet, username, password);
        }

        HttpParams httpParams = new BasicHttpParams();
        httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
        httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
        httpParams.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, socketBufferSize);

        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        return httpResponse;
    } catch (IOException ex) {
        logger.error(ex);
        return null;
    } catch (AuthenticationException ex) {
        logger.error(ex);
        return null;
    }
}

0 个答案:

没有答案