httpclient:HttpClient示例详解

 2021-07-13 0:03    77  

是 Apache Jakarta Common 下的子项目,是一个 协议的客户端编程工具包,并且它支持 }

httpclient:HttpClient示例详解

的主要功能实现了所有 、POST、PUT、HEAD、DELETE、HEAD、OPTIONS 等)

httpclient:HttpClient示例详解

支持 协议支持代理服务器(Nginx等)等支持自动(跳转)转向 开发相关包

httpclient:HttpClient示例详解

<!-- ://mvnrepository.com/artifact/org.apache./ --><dependency> <groupId>org.apache.</groupId> <artifactId></artifactId> <version>4.5.13</version></dependency><!-- ://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version></dependency>GET无参示例public void getNoParameterFunction() { // 获得,相当于打开一个浏览器 Closeable = ().build(); // 创建Get请求 = new (":8080/XXB_PROJECT/szhzx/pc/hotnews/list"); // 响应模型 Closeable response = null; try { //客户端执行Get请求 response = (); //从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态为:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }运行结果:

GET有参数方式一通过URL拼接的方式,把URL拼成一个完整的URL,然后发送请求;

@Test public void getHaveParameterFunction() { //获得 = ().build(); //设置参数 StringBuffer params = new StringBuffer(); try { //如果使用编解码方式,需要使用URLEncoder.encode("1f8193a43bab4a409e36475addb286ba", "utf-8") params.append("news_id=1f8193a43bab4a409e36475addb286ba"); params.append("&"); params.append("mark=1"); } catch (Exception e1) { e1.printStackTrace(); } // 创建Get请求 = new (":8080/XXB_PROJECT/szhzx/pc/news/detail" + "?" + params); // 响应模型 Closeable response = null; try { // 配置信息 RequestConfig requestConfig = RequestConfig.custom() // 设置连接超时时间(单位毫秒) .setConnectTimeout(5000) // 设置请求超时时间(单位毫秒) .setConnectionRequestTimeout(5000) // socket读写超时时间(单位毫秒) .setSocketTimeout(5000) // 设置是否允许重定向(默认为true) .setRedirectsEnabled(true).build(); // 将上面的配置信息 运用到这个Get请求里 (requestConfig); // 由客户端执行(发送)Get请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态为:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }运行结果:

Get有参数方式二通过java.net.URI设置参数,获取

@Test public void getHaveParameterFunction() { // 获得 = ().build(); // 设置参数 java.net.URI uri = null; try { /* * 将参数放入键值对类NameValuePair中,再放入集合中 * 注:这里也支持一个键值对一个键值对地往里存放setParameter(String key, String value) */ List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("news_id", "1f8193a43bab4a409e36475addb286ba")); uri = new URIBuilder().setScheme(";).setHost("localhost").setPort(8080).setPath("/XXB_PROJECT/szhzx/pc/news/detail") .setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } // 创建Get请求 = new (uri); // 响应模型 Closeable response = null; try { // 配置信息 RequestConfig requestConfig = RequestConfig.custom() // 设置连接超时时间(单位毫秒) .setConnectTimeout(5000) // 设置请求超时时间(单位毫秒) .setConnectionRequestTimeout(5000) // socket读写超时时间(单位毫秒) .setSocketTimeout(5000) // 设置是否允许重定向(默认为true) .setRedirectsEnabled(true).build(); // 将上面的配置信 运用到这个Get请求里 (requestConfig); // 由客户端执行Get请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态为:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }运行结果:

Post无参数@Test public void postHaveParameterFunction() { // 获得 = ().build(); // 创建Post请求 = new (":8080/XXB_PROJECT/szhzx/pc/news/detail"); // 响应模型 Closeable response = null; try { // 由客户端执行(发送)Post请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }Post有参数方式一通过URL拼接的方式,把接口地址和参数拼接成一个过多整的地址后,再发送具体的请求

@Test public void postHaveParameterFunction() { // 获得 = ().build(); StringBuffer params = new StringBuffer(); try { //如果使用编解码方式,需要使用URLEncoder.encode("1f8193a43bab4a409e36475addb286ba", "utf-8") params.append("news_id=1f8193a43bab4a409e36475addb286ba"); params.append("&"); params.append("mark=1"); } catch (Exception e1) { e1.printStackTrace(); } // 创建Post请求 = new (":8080/XXB_PROJECT/szhzx/pc/news/detail" + "?" + params); // 设置ContentType ("Content-Type", "application/json;charset=utf8"); // 响应模型 Closeable response = null; try { // 由客户端执行(发送)Post请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }Post有参数方式二@Test public void postHaveParameterFunction() { // 获得 = ().build(); // 创建Post请求 = new (":8080/XXB_PROJECT/szhzx/pc/news/detail"); User user = new User(); user.setName("张二"); user.setCompany("BAT"); // 我这里利用阿里的fastjson,将Object转换为json字符串; // (需要导入com.alibaba.fastjson.JSON包) String jsonString = JSON.toJSONString(user); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 (entity); ("Content-Type", "application/json;charset=utf8"); // 响应模型 Closeable response = null; try { // 由客户端执行(发送)Post请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }Post有参数方式三@Test public void postHaveParameterFunction() { // 获得 = ().build(); // 创建Post请求 URI uri = null; try { // 将参数放入键值对类NameValuePair中 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("news_id", "1f8193a43bab4a409e36475addb286ba")); params.add(new BasicNameValuePair("mark", "1")); uri = new URIBuilder().setScheme(";).setHost("localhost").setPort(8080) .setPath("XXB_PROJECT/szhzx/pc/news/detail").setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } = new (uri); //设置参数 User user = new User(); user.setName("张二"); user.setCompany("BAT"); // 将user对象转换为json字符串,并放入entity中 StringEntity entity = new StringEntity(JSON.toJSONString(user), "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 (entity); ("Content-Type", "application/json;charset=utf8"); // 响应模型 Closeable response = null; try { // 由客户端执行(发送)Post请求 response = (); // 从响应模型中获取响应实体 responseEntity = response.getEntity(); System.out.println("响应状态:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度:" + responseEntity.getContentLength()); System.out.println("响应内容:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if ( != null) { (); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }

本文标签:详解HttpClient示例

原文链接:https://www.xgfox.com/alpx/818.html

本文版权:如无特别标注,本站文章均为原创。