美文网首页
简单的HTTP请求及response处理

简单的HTTP请求及response处理

作者: 萝卜的琪迹 | 来源:发表于2017-05-17 11:40 被阅读0次

最近工作中需要对接API,需要用到HTTP请求及处理返回response,特此记录。

创建连接,几种不同的创建方式
HttpClient client = HttpClientBuilder.create().build(); // HttpClient client = HttpClients.createDefault(); // CloseableHttpClient client = HttpClients.createDefault();

初始化URL
String url = "http://localhost:8080/imcallback"; URL target = null; try { target = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); }

创建request
HttpUriRequest request = null; // method try { request = new HttpPost(target.toURI()); // request = new HttpPut(target.toURI()); // request = new HttpGet(target.toURI()); // request = new HttpDelete(target.toURI()); } catch (URISyntaxException e) { e.printStackTrace(); }

给request添加header
for (NameValuePair nameValuePair : header.getHeaders()) { request.addHeader(nameValuePair.getName(), nameValuePair.getValue()); }

给request添加body(具体包含json格式字符串还是其他格式需自行处理)
if (null != body) { ((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(body.toString(), "UTF-8")); }

创建response,执行并获得response
HttpResponse response; //CloseableHttpResponse response = null; try { response = client.execute(request); } catch (IOException e) { e.printStackTrace(); }

处理状态码
int statusCode = response.getStatusLine().getStatusCode();

获得内容
HttpEntity entity = response.getEntity(); //获得JSON对象 ObjectNode responseNode = (ObjectNode) EntityUtils.toString(response.getEntity(), "UTF-8");
具体JSON的处理用到了jackson。

最后关闭
finally { try { response.close(); client.close(); } catch (Exception e) { e.printStackTrace(); }

需要httpclient4.3.3以上,maven配置如下:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
<exclusions>

<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.3.3</version>
<exclusions>

<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.3</version>
<exclusions>

<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

相关文章

  • 简单的HTTP请求及response处理

    最近工作中需要对接API,需要用到HTTP请求及处理返回response,特此记录。 创建连接,几种不同的创建方式...

  • ASP.NET Core 中间件

    1.前言 整个HTTP Request请求跟HTTP Response返回结果之间的处理流程是一个请求管道(req...

  • 如何实现一个http服务器

    1. HTTP协议 HTTP请求(Request) 请求行 消息报头 请求正文 HTTP响应(Response) ...

  • 常用内置函数三

    一、Python的标准Web库 urllib是基于http高层的库 request处理客户端请求 response...

  • 前端技术前沿7

    服务器是如何处理请求的 使用response.writeHead()函数发送一个HTTP状态200和HTTP头的内...

  • 服务器处理请求

    request和response都是服务器创建的对象参考文章Http协议 服务器处理请求流程服务器每次收到请求时,...

  • response入门

    response response对象表示对程序发出的http请求的响应。 response对象常用属性和方法 r...

  • Linux命令之curl

    获取http请求头信息,-I 只打印response header, -i 打印response header和...

  • HTTP请求到response

    从点击到呈现 — 详解一次HTTP请求(1) 其实想这个内容很久了,一方面是回顾自己所学的 http 知识,另外一...

  • HttpUtil工具

    HttpUtil工具,http get post请求,https get post请求,ajax response...

网友评论

      本文标题:简单的HTTP请求及response处理

      本文链接:https://www.haomeiwen.com/subject/mqqdxxtx.html