继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

HttpClicent学习-会补充

慕粉2139185169
关注TA
已关注
手记 30
粉丝 27
获赞 128

基于:httpclient 4.5.3
1、httpclient的获得
1.1 httpclients
static CloseableHttpClient createDefault()
Creates CloseableHttpClient instance with default configuration.
static CloseableHttpClient createMinimal()
Creates CloseableHttpClient instance that implements the most basic HTTP
protocol support.
static CloseableHttpClient createMinimal(HttpClientConnectionManager connManager)
Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.
static CloseableHttpClient createSystem()
Creates CloseableHttpClient instance with default configuration based on ssytem properties.
static HttpClientBuilder custom()
Creates builder object for construction of custom CloseableHttpClient instances.--创建自定义的httpclient
1.2 httpclientUtils
static void closeQuietly(CloseableHttpResponse response)
Unconditionally close a response.
static void closeQuietly(HttpClient httpClient)
Unconditionally close a httpClient.
static void closeQuietly(org.apache.http.HttpResponse response)
Unconditionally close a response.
public static void closeQuietly(org.apache.http.HttpResponse response)
Unconditionally close a response.
Example Code:

HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
} catch (Exception e) {
// error handling
} finally {
HttpClientUtils.closeQuietly(httpResponse);
}

Parameters:
response - the HttpResponse to release resources, may be null or already closed.
Since:
4.2

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

   HttpUriRequest request=RequestBuilder.put("https://10.120.xx.xx:port/itpaas/uam/auth/token")

                .setEntity(entity)

                .setHeader("Content-Type", "application/json; charset=UTF-8")

                .setHeader("language", "CH/EN")

                .build();

        CloseableHttpResponse response = httpclient.execute(request);

        

        System.out.println(response.getStatusLine());

        System.out.println(EntityUtils.toString(response.getEntity()));


-------------main方法中写入-------------<需要使用user+pass获取token>

CloseableHttpClient httpclient =HttpClients.custom()

                .setSSLSocketFactory(sslsf)

                .setConnectionManager(cm)

                .setConnectionManagerShared(true)

                .build();

        HttpEntity entity = EntityBuilder.create()

                .setText("{\"userid\":\"admin\",\"pass\":\"password\"}")//转义"

                .setContentType(ContentType.DEFAULT_TEXT)

                .build();

     entity对象可以研究下

  sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);

            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()

                    .register("http", new PlainConnectionSocketFactory())

                    .register("https", sslsf)

                    .build();

            cm = new PoolingHttpClientConnectionManager(registry);

            cm.setMaxTotal(200);//max connection

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


查看全部评论