Apache HttpClient 4 3开发指南

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

《Apache HttpClient 4.3开发指南》

作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs

一、概述

Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。

本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。

要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。

二、开发手册

1、创建HTTP客户端
CloseableHttpClient client = HttpClientBuilder.create().build();

2、发送基本的GET请求
instance.execute(new HttpGet(“http://www.baidu.com”));

3、获取HTTP响应的状态码
String url = “http://www.baidu.com”;CloseableHttpResponse response = instance.execute(new HttpGet(url));assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

4、获取响应的媒体类型
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()));

5、获取响应的BODY部分
String url = “http://www.baidu.com”;CloseableHttpResponse response = instance.execute(new HttpGet(url));String bodyAsString = EntityUtils.toString(response.getEntity());assertThat(bodyAsString, notNullValue());

6、配置请求的超时设置
@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);}

7、发送POST请求
instance.execute(new HttpPost(SAMPLE_URL));

8、为HTTP请求配置重定向
CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));

9、配置请求的HEADER部分
HttpGet request = new HttpGet(SAMPLE_URL);request.addHeader(HttpHeaders.ACCEPT, “application/xml”);response = instance.execute(request);

10、获取响应的HEADER部分
CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);assertThat(headers, not(emptyArray()));

11、关闭或释放资源
response = instance.execute(new HttpGet(SAMPLE_URL));try{  HttpEntity entity = response.getEntity();  if(entity!=null){InputStream instream = entity.getContent();instream.close();  }} finally{  response.close();}

以上内容涵盖了HttpClient 4.3所有常见的需求,供开发者参考。


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.apache.http.ConnectionClosedException.class org.apache.http.ConnectionReuseStrategy.class org.apache.http.Consts.class org.apache.http.ContentTooLongException.class org.apache.http.ExceptionLogger.class org.apache.http.FormattedHeader.class org.apache.http.Header.class org.apache.http.HeaderElement.class org.apache.http.HeaderElementIterator.class org.apache.http.HeaderIterator.class org.apache.http.HttpClientConnection.class org.apache.http.HttpConnection.class org.apache.http.HttpConnectionFactory.class org.apache.http.HttpConnectionMetrics.class org.apache.http.HttpEntity.class org.apache.http.HttpEntityEnclosingRequest.class org.apache.http.HttpException.class org.apache.http.HttpHeaders.class org.apache.http.HttpHost.class org.apache.http.HttpInetConnection.class org.apache.http.HttpMessage.class org.apache.http.HttpRequest.class org.apache.http.HttpRequestFactory.class org.apache.http.HttpRequestInterceptor.class org.apache.http.HttpResponse.class org.apache.http.HttpResponseFactory.class org.apache.http.HttpResponseInterceptor.class org.apache.http.HttpServerConnection.class org.apache.http.HttpStatus.class org.apache.http.HttpVersion.class org.apache.http.MalformedChunkCodingException.class org.apache.http.MessageConstraintException.class org.apache.http.MethodNotSupportedException.class org.apache.http.NameValuePair.class org.apache.http.NoHttpResponseException.class org.apache.http.ParseException.class org.apache.http.ProtocolException.class org.apache.http.ProtocolVersion.class org.apache.http.ReasonPhraseCatalog.class org.apache.http.RequestLine.class org.apache.http.StatusLine.class org.apache.http.TokenIterator.class org.apache.http.TruncatedChunkException.class org.apache.http.UnsupportedHttpVersionException.class org.apache.http.annotation.Contract.class org.apache.http.annotation.Experimental.class org.apache.http.annotation.Obsolete.class org.apache.http.annotation.ThreadingBehavior.class org.apache.http.annotation.package-info.class org.apache.http.concurrent.BasicFuture.class org.apache.http.concurrent.Cancellable.class org.apache.http.concurrent.FutureCallback.class org.apache.http.concurrent.package-info.class org.apache.http.config.ConnectionConfig.class org.apache.http.config.Lookup.class org.apache.http.config.MessageConstraints.class org.apache.http.config.Registry.class org.apache.http.config.RegistryBuilder.class org.apache.http.config.SocketConfig.class org.apache.http.config.package-info.class org.apache.http.entity.AbstractHttpEntity.class org.apache.http.entity.BasicHttpEntity.class org.apache.http.entity.BufferedHttpEntity.class org.apache.http.entity.ByteArrayEntity.class org.apache.http.entity.ContentLengthStrategy.class org.apache.http.entity.ContentProducer.class org.apache.http.entity.ContentType.class org.apache.http.entity.EntityTemplate.class org.apache.http.entity.FileEntity.class org.apache.http.entity.HttpEntityWrapper.class org.apache.http.entity.InputStreamEntity.class org.apache.http.entity.SerializableEntity.class org.apache.http.entity.StringEntity.class org.apache.http.entity.package-info.class org.apache.http.impl.AbstractHttpClientConnection.class org.apache.http.impl.AbstractHttpServerConnection.class org.apache.http.impl.BHttpConnectionBase.class org.apache.http.impl.ConnSupport.class org.apache.http.impl.DefaultBHttpClientConnection.class org.apache.http.impl.DefaultBHttpClientConnectionFactory.class org.apache.http.impl.DefaultBHttpServerConnection.class org.apache.http.impl.DefaultBHttpServerConnectionFactory.class org.apache.http.impl.DefaultConnectionReuseStrategy.class org.apache.http.impl.DefaultHttpClientConnection.class org.apache.http.impl.DefaultHttpRequestFactory.class org.apache.http.impl.DefaultHttpResponseFactory.class org.apache.http.impl.DefaultHttpServerConnection.class org.apache.http.impl.EnglishReasonPhraseCatalog.class org.apache.http.impl.HttpConnectionMetricsImpl.class org.apache.http.impl.NoConnectionReuseStrategy.class org.apache.http.impl.SocketHttpClientConnection.class org.apache.http.impl.SocketHttpServerConnection.class org.apache.http.impl.bootstrap.HttpServer.class org.apache.http.impl.bootstrap.RequestListener.class org.apache.http.impl.bootstrap.SSLServerSetupHandler.class org.apache.http.impl.bootstrap.ServerBootstrap.class org.apache.http.impl.bootstrap.ThreadFactoryImpl.class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值