# zhttp **Repository Path**: zhu-yinli/zhttp ## Basic Information - **Project Name**: zhttp - **Description**: zhttp是一个封装了httpcomponents的工具,用于简化httpcomponents的操作。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2021-05-27 - **Last Updated**: 2022-07-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README zhttp是一个封装了httpcomponents的工具,用于简化httpcomponents的操作。 用httpcomponents发送一个get请求,至少需要这样: CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // Create a custom response handler ResponseHandler responseHandler = new ResponseHandler() { private HttpResponse response; @Override public String handleResponse(final HttpResponse response) throws IOException { this.response = response; int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity, "UTF-8") : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody; try { responseBody = httpClient.execute(httpGet, responseHandler); } catch (IOException e) { throw new HttpUtilsException("Get from " + url + " error.", e); } finally { httpClient.close(); } 用zhttp封装后,现在发送一个get请求,只需要以下几步: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); String responseBody; try { responseBody = (String) basicHttpUtils.get(); } catch (HttpUtilsException e) { throw new ServiceException("Get from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Get from " + url + " error.", e); } zhttp还提供了发送post请求、下载文件、发送带Authorization Header的HTTP请求、在请求体中带上表单数据、在请求体中带上文本或json数据等功能。 下面我们介绍zhttp的使用。 一、在项目中引用zhttp; 1.1 下载“zjson-1.7.jar”到本地(下载地址:https://gitee.com/zhu-yinli/zjson/attach_files/714219/download/zjson-1.7.jar); 1.2 用命令将zjson安装到本地maven仓库; mvn install:install-file -Dfile=D:\idea-workspace\zjson\target\zjson-1.7.jar -DgroupId=com.halo -DartifactId=zjson -Dversion=1.7 -Dpackaging=jar 1.3 在pom.xml文件中加入引用(在build-pluginManagement-plugins小节,如果已有maven-install-plugin就不需要再加了): org.apache.maven.plugins maven-install-plugin 2.5.2 1.4 在pom.xml文件中加入以下依赖(在dependencies小节): net.sf.json-lib json-lib 2.4 jdk15 com.halo zjson 1.7 1.5 下载zhttp-2.4.jar到本地(下载地址:https://gitee.com/zhu-yinli/zhttp/attach_files/720428/download/zhttp-2.4.jar); 1.6 用以下命令将zhttp安装到本地maven仓库; mvn install:install-file -Dfile=D:\idea-workspace\zhttp\target\zhttp-2.4.jar -DgroupId=com.halo -DartifactId=zhttp -Dversion=2.4 -Dpackaging=jar 1.7 在pom.xml文件中加入以下依赖(在dependencies小节): org.apache.httpcomponents httpcore 4.4.10 org.apache.httpcomponents httpclient 4.5.6 com.halo zhttp 2.4 二、zhttp的使用; 2.1 最简单的GET请求上面已经介绍过,现在来看看URL后带参数的get请求: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); Map args = new HashMap<>(); args.put("account", account); args.put("accessToken", accessToken); args.put("client", "h5"); basicHttpUtils.setArgs(args); String responseBody; try { responseBody = (String) basicHttpUtils.get(); } catch (HttpUtilsException e) { throw new ServiceException("Get from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Get from " + url + " error.", e); } 2.2 POST请求,将上面的“get”换成“post”即可: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); Map args = new HashMap<>(); args.put("account", account); args.put("accessToken", accessToken); args.put("client", "h5"); basicHttpUtils.setArgs(args); String responseBody; try { responseBody = (String) basicHttpUtils.post(); } catch (HttpUtilsException e) { throw new ServiceException("Post from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Post from " + url + " error.", e); } 2.3 HTTP表单里带数据的POST请求: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); AssemblyLine assemblyLine = basicHttpUtils.getAssemblyLine(); if (null == assemblyLine) { assemblyLine = new AssemblyLine(); basicHttpUtils.setAssemblyLine(assemblyLine); } List formData = new ArrayList<>(); formData.add(new BasicNameValuePair("grant_type", "authorization_code")); formData.add(new BasicNameValuePair("code", code)); formData.add(new BasicNameValuePair("redirect_uri", redirectUrl)); FormDataAssembler formDataAssembler = new FormDataAssembler(formData); assemblyLine.addAssembler(formDataAssembler); String responseBody; try { responseBody = (String) basicHttpUtils.post(); } catch (HttpUtilsException e) { throw new ServiceException("Post from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Post from " + url + " error.", e); } 2.4 在请求体中带上文本或json数据的POST请求: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); AssemblyLine assemblyLine = basicHttpUtils.getAssemblyLine(); if (null == assemblyLine) { assemblyLine = new AssemblyLine(); basicHttpUtils.setAssemblyLine(assemblyLine); } RequestBodyAssembler requestBodyAssembler = new RequestBodyAssembler("{ "id": 1 }", "application/json"); assemblyLine.addAssembler(formDataAssembler); String responseBody; try { responseBody = (String) basicHttpUtils.post(); } catch (HttpUtilsException e) { throw new ServiceException("Post from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Post from " + url + " error.", e); } 2.5 带Authorization Header的HTTP请求,目前只实现了Basic验证: BasicHttpUtils basicHttpUtils = new BasicHttpUtils(String.class, url); AssemblyLine assemblyLine = basicHttpUtils.getAssemblyLine(); if (null == assemblyLine) { assemblyLine = new AssemblyLine(); basicHttpUtils.setAssemblyLine(assemblyLine); } String clientId = "25841d1961f25b427fc8"; String clientSecret = "fd57902b2fb7243f524814ab640361"; BasicAuthBuilder basicAuthBuilder = new BasicAuthBuilder(clientId, clientSecret); AuthorizationHeaderAssembler authHeaderAssembler = new AuthorizationHeaderAssembler(basicAuthBuilder); assemblyLine.addAssembler(authHeaderAssembler); String responseBody; try { responseBody = (String) basicHttpUtils.post(); } catch (HttpUtilsException e) { throw new ServiceException("Post from " + url + " error.", e); } catch (IOException e) { throw new ServiceException("Post from " + url + " error.", e); } 2.6 使用“AdvanceHttpUtils”,可以将Java对象(POJO对象)转化成json,并将返回结果的json转化成Java对象: AdvanceHttpUtils advanceHttpUtils = new AdvanceHttpUtils(url); advanceHttpUtils.setRequestJsonUtils(new JSONUtils(AccessTokenRequestBean.class)); AccessTokenRequestBean accessTokenRequestBean = new AccessTokenRequestBean(); accessTokenRequestBean.setUserId(userId); accessTokenRequestBean.setPassword(password); advanceHttpUtils.setRequestJsonBean(accessTokenRequestBean); advanceHttpUtils.setResponseJsonUtils(new JSONUtils(AccessTokenResponseBean.class)); AccessTokenResponseBean accessTokenResponseBean; try { accessTokenResponseBean = (AccessTokenResponseBean) advanceHttpUtils.post(); } catch (HttpUtilsException e) { throw new ServiceException("Get login access token error.", e); } catch (IOException e) { throw new ServiceException("Get login access token error.", e); } 2.7 下载文件: BasicHttpUtils basicHttpUtils = new BasicHttpUtils<>(File.class, url); File downloadFile = null; try { downloadFile = (File) basicHttpUtils.get(); } catch (HttpUtilsException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(downloadFile.getAbsolutePath()); https://gitee.com/zhu-yinli/zhttp