作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs
Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。
本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。
要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。
CloseableHttpClient client = HttpClientBuilder.create().build();
instance.execute(new HttpGet(“http://www.baidu.com”));
String url = “http://www.baidu.com”;CloseableHttpResponse response = instance.execute(new HttpGet(url));assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
String url = “http://www.baidu.com”;CloseableHttpResponse response = instance.execute(new HttpGet(url));String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
String url = “http://www.baidu.com”;CloseableHttpResponse response = instance.execute(new HttpGet(url));String bodyAsString = EntityUtils.toString(response.getEntity());assertThat(bodyAsString, notNullValue());
@Test(expected=SocketTimeoutException.class)public void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException{ RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(50).setConnectTimeout(50) .setSocketTimeout(50).build(); HttpGet request = new HttpGet(SAMPLE_URL); request.setConfig(requestConfig); instance.execute(request);}
instance.execute(new HttpPost(SAMPLE_URL));
CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));
HttpGet request = new HttpGet(SAMPLE_URL);request.addHeader(HttpHeaders.ACCEPT, “application/xml”);response = instance.execute(request);
CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);assertThat(headers, not(emptyArray()));
response = instance.execute(new HttpGet(SAMPLE_URL));try{ HttpEntity entity = response.getEntity(); if(entity!=null){InputStream instream = entity.getContent();instream.close(); }} finally{ response.close();}
apache部署web项目,
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态